NodeJS实现IOS中的APNS

生成证书

导出

打开钥匙串:
钥匙串 -> My Certificates 导出对应的Push证书 (cer格式)
钥匙串 -> My Certificates 展开私钥 导出对应的证书
密码输入:123456

生成PEM

Push证书 p12格式
终端输入
1,openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
2,openssl pkcs12 -in key.p12 -out key.pem -nodes

NodeJS代码

安装apn

npm install apn

编写代码:

IOS注册省略

大专栏   NodeJS实现IOS中的APNS
var apn = require("apn");

var tokens = ["e73447af823400987c05f31a2b5a0960a729ceaea9d772869f0f22eb463bea8c"];

var service = new apn.Provider({
  "cert": "push/cert.pem",
  "key": "push/key.pem",
  "passphrase":"123456",
  "production":false
});

var note = new apn.Notification({
  "alert":"末世战纪欢迎您的到来",
  "sound":"default"
});

// The topic is usually the bundle identifier of your application.
note.topic = "com.eray.off";
note.badge = 1;

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();

猜你喜欢

转载自www.cnblogs.com/liuzhongrong/p/12390564.html