【发布时间】:2016-08-06 14:27:47
【问题描述】:
我正在寻找创建 APNS(Apple 推送通知服务),服务器将在其中向 iOS 设备发送通知。 我可以使用 SAME 设备令牌和 SAME 证书通过 PHP 进行推送通知,但是,我想通过 Node JS 而不是 PHP 发送通知。
我有以下有效文件/证书可以帮助我入门:
- cert.pem
- key.pem
- aps_development.cer
- cert.p12
- key.p12,
- ck.pem
我一直在浏览多个资源/链接,例如:
这样做之后,我能够想出以下示例代码,其中 PASSWORD 代表 key.pem 的密码,TOKEN 代表我设备的令牌:
var apn = require("apn");
var path = require('path');
try {
var options = {
cert: path.join(__dirname, 'cert.pem'), // Certificate file path
key: path.join(__dirname, 'key.pem'), // Key file path
passphrase: '<PASSWORD>', // A passphrase for the Key file
ca: path.join(__dirname, 'aps_development.cer'),// String or Buffer of CA data to use for the TLS connection
production:false,
gateway: 'gateway.sandbox.push.apple.com', // gateway address
port: 2195, // gateway port
enhanced: true // enable enhanced format
};
var apnConnection = new apn.Connection(options);
var myDevice = new apn.Device("<TOKEN>");
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 3;
note.sound = "ping.aiff";
note.alert = "You have a new message";
note.payload = {'msgFrom': 'Alex'};
note.device = myDevice;
apnConnection.pushNotification(note);
process.stdout.write("******* EXECUTED WITHOUT ERRORS************ :");
} catch (ex) {
process.stdout.write("ERROR :"+ex);
}
执行此代码时我没有收到任何错误,但问题是我的 iOS 设备上没有收到任何通知。我也尝试过设置 ca:null & debug:true (在选项 var 中)。但同样的事情也会发生。
再次,当我使用我拥有的 ck.pem 和设备令牌并将其与 PHP 一起使用时,它可以工作,但我无法使其在 Node JS 中工作。请帮忙!!
非常感谢!
【问题讨论】:
标签: ios node.js apple-push-notifications apn