基于HTTP2与Universal-Push-Notification-Client-SSL证书方式开发APNs推送

简介

新的APNs协议基于HTTP2,一种是使用Universal Push Notification Client SSL 证书,一种是使用Token,本文主要将使用证书方式编程

代码实例

/**
 * 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 传入有两种方式,一种是通过文件路径,另一种是数据流
// p12 文件需要在苹果开发者网站进行申请
const options = {
  pfx: pfx_stream, // pfx: "./test1.163.com.p12"
  passphrase: "12345",
  production: false
};

// 此token是 app注册苹果服务器时,苹果服务器返回的唯一的标示符,如果是使用mac进行开发,在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 文件对应,可以通过解析 pfx 文件获取到,对应解析出来的 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: []
[]

你可能感兴趣的:(基于HTTP2与Universal-Push-Notification-Client-SSL证书方式开发APNs推送)