基于HTTP2與Universal-Push-Notification-Client-SSL證書方式開發(fā)APNs推送

簡介

新的APNs協(xié)議基于HTTP2,一種是使用Universal Push Notification Client SSL 證書,一種是使用Token,本文主要將使用證書方式編程

代碼實(shí)例

/**
 * Created by tianyuan on 2017/6/8.
 */

'use strict';

const apn = require("apn");
const fs = require("fs");

const pfx_stream = fs.readFileSync("./test1.163.com.p12");

// pfx 傳入有兩種方式,一種是通過文件路徑,另一種是數(shù)據(jù)流
// p12 文件需要在蘋果開發(fā)者網(wǎng)站進(jìn)行申請
const options = {
  pfx: pfx_stream, // pfx: "./test1.163.com.p12"
  passphrase: "12345",
  production: false
};

// 此token是 app注冊蘋果服務(wù)器時,蘋果服務(wù)器返回的唯一的標(biāo)示符,如果是使用mac進(jìn)行開發(fā),在xcode里面可以獲取到此字符串
const tokens = ['f6d9c6f25bd7b7ac63c7c79557dcbf64b91c0480758836db5e00bdc31f6cfecc'];

const service = new apn.Provider(options);

let note = new apn.Notification({
  alert:  "Breaking News: I just sent my first Push Notification",
});

// The topic is usually the bundle identifier of your application.
// 此 topic 必須和 pfx 文件對應(yīng),可以通過解析 pfx 文件獲取到,對應(yīng)解析出來的 userId
note.topic = "com.netease.pomelo.apnstest";

console.log(`Sending: ${note.compile()} to ${tokens}`);

service.send(note, tokens).then( result => {
  console.log("sent:", result.sent.length);
  console.log("failed:", result.failed.length);
  console.log(result.failed);
});

// For one-shot notification tasks you may wish to shutdown the connection
// after everything is sent, but only call shutdown if you need your
// application to terminate.
service.shutdown();

package.json 中添加
"apn": "2.1.4"

錯誤示例

  • p12 文件錯誤
tianyuandeMacBook-Pro:nodejs-test tianyuan$ node test_apns_p12.js
Sending: {"aps":{"alert":"Breaking News: I just sent my first Push Notification"}} to f6d9c6f25bd7b7ac63c7c79557dcbf64b91c0480758836db5e00bdc31f6cfecc
sent: []
failed: [ { device: 'f6d9c6f25bd7b7ac63c7c79557dcbf64b91c0480758836db5e00bdc31f6cfecc',
    error:
     Error: mac verify failure
         at Error (native)
         at Object.createSecureContext (_tls_common.js:137:17)
         at Object.exports.connect (_tls_wrap.js:1033:48)
         at EventEmitter.connect [as _connect] (/Users/tianyuan/Workspace/nodejs-test/node_modules/apn/lib/protocol/endpoint.js:80:24)
         at EventEmitter.Endpoint (/Users/tianyuan/Workspace/nodejs-test/node_modules/apn/lib/protocol/endpoint.js:38:10)
         at EventEmitter.createEndpoint (/Users/tianyuan/Workspace/nodejs-test/node_modules/apn/lib/protocol/endpointManager.js:51:22)
         at EventEmitter.getStream (/Users/tianyuan/Workspace/nodejs-test/node_modules/apn/lib/protocol/endpointManager.js:32:12)
         at resolve (/Users/tianyuan/Workspace/nodejs-test/node_modules/apn/lib/client.js:121:43)
         at Client.getStream (/Users/tianyuan/Workspace/nodejs-test/node_modules/apn/lib/client.js:120:12)
         at Client.write (/Users/tianyuan/Workspace/nodejs-test/node_modules/apn/lib/client.js:41:17) } ]
  • topic 錯誤
tianyuandeMacBook-Pro:nodejs-test tianyuan$ node test_apns_p12.js
Sending: {"aps":{"alert":"Breaking News: I just sent my first Push Notification"}} to f6d9c6f25bd7b7ac63c7c79557dcbf64b91c0480758836db5e00bdc31f6cfecc
sent: []
failed: [ { device: 'f6d9c6f25bd7b7ac63c7c79557dcbf64b91c0480758836db5e00bdc31f6cfecc',
    status: '400',
    response: { reason: 'TopicDisallowed' } } ]
[ { device: 'f6d9c6f25bd7b7ac63c7c79557dcbf64b91c0480758836db5e00bdc31f6cfecc',
    status: '400',
    response: { reason: 'TopicDisallowed' } } ]
  • 成功情況
tianyuandeMacBook-Pro:nodejs-test tianyuan$ node test_apns_p12.js
Sending: {"aps":{"alert":"Breaking News: I just sent my first Push Notification"}} to f6d9c6f25bd7b7ac63c7c79557dcbf64b91c0480758836db5e00bdc31f6cfecc
sent: [ { device: 'f6d9c6f25bd7b7ac63c7c79557dcbf64b91c0480758836db5e00bdc31f6cfecc' } ]
failed: []
[]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,551評論 19 139
  • npm install -g hexo-clisudo npm install -g hexo-clinode -...
    iOS的Developer閱讀 14,768評論 2 0
  • 我給我們家小學(xué)生布置每周寫一篇作文,主題我選,周日交給我改。 瑛是第一個寫完的,主題是爸爸媽媽中選一個寫,晚上九點(diǎn)...
    繁櫻之處灼灼銀花閱讀 985評論 0 0
  • 有一天,我突然想到這個問題~我是個無趣的人嗎?是的,我覺得自己和別人一起時好像不知道該如何聊天;我覺得自己的興趣愛...
    orial閱讀 503評論 0 1
  • 他步出隔間,沿著走廊走到茶水間的窗前立定。 他深深地吸了一口氣,頃刻——他的三分之二個肺涌入了冬晨的冷空氣,余下的...
    EncyKe閱讀 178評論 0 0

友情鏈接更多精彩內(nèi)容