基于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: []
[]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • npm install -g hexo-clisudo npm install -g hexo-clinode -...
    iOS的Developer阅读 14,659评论 2 0
  • 我给我们家小学生布置每周写一篇作文,主题我选,周日交给我改。 瑛是第一个写完的,主题是爸爸妈妈中选一个写,晚上九点...
    繁樱之处灼灼银花阅读 940评论 0 0
  • 有一天,我突然想到这个问题~我是个无趣的人吗?是的,我觉得自己和别人一起时好像不知道该如何聊天;我觉得自己的兴趣爱...
    orial阅读 396评论 0 1
  • 他步出隔间,沿着走廊走到茶水间的窗前立定。 他深深地吸了一口气,顷刻——他的三分之二个肺涌入了冬晨的冷空气,余下的...
    EncyKe阅读 166评论 0 0