簡介
新的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: []
[]