带有节点JS的APNS(Apple推送通知服务)

我正在寻找创buildAPNS(苹果推送通知服务),在那里服务器将发送通知到iOS设备。 我能够通过PHP使用SAME设备令牌和相同的证书使推送通知工作,但是,我想通过节点JS而不是PHP发送通知。

我有以下有效的文件/证书来帮助我开始:

  • cert.pem
  • key.pem
  • aps_development.cer
  • cert.p12
  • key.p12,
  • ck.pem

我一直在查看几个资源/链接,如:

  • https://github.com/argon/node-apn
  • 如何通过nodejs实现APNS通知?

这样做之后,我能够拿出下面的示例代码,其中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(在options var中)。 但同样的事情发生。

再次,当我使用ck.pem&device令牌,并使用它与PHP,它的工作原理,但我不能让它在Node JS中工作。 请帮忙!!

非常感谢!

您可能正在运行NodeJS本身的asynchronous特性。 我使用了相同的node-apn模块,并取得了巨大的成功。 但是你不要直接像在PHP中那样直接调用它 – 这是一个不能从PHP-> Node进行映射的同步模型。 你的过程在任何事情发生之前都会退出 – apnConnection.pushNotification(note); 是一个asynchronous调用,只是在你的脚本返回/退出之前刚开始。

正如在node-apn文档中提到的,您可能想要“在apnConnection上监听”其他事件。 下面是我用来注销创build后在连接上发生的各种事件的代码摘录:

 // We were unable to initialize the APN layer - most likely a cert issue. connection.on('error', function(error) { console.error('APNS: Initialization error', error); }); // A submission action has completed. This just means the message was submitted, not actually delivered. connection.on('completed', function(a) { console.log('APNS: Completed sending', a); }); // A message has been transmitted. connection.on('transmitted', function(notification, device) { console.log('APNS: Successfully transmitted message'); }); // There was a problem sending a message. connection.on('transmissionError', function(errorCode, notification, device) { var deviceToken = device.toString('hex').toUpperCase(); if (errorCode === 8) { console.log('APNS: Transmission error -- invalid token', errorCode, deviceToken); // Do something with deviceToken here - delete it from the database? } else { console.error('APNS: Transmission error', errorCode, deviceToken); } }); connection.on('connected', function() { console.log('APNS: Connected'); }); connection.on('timeout', function() { console.error('APNS: Connection timeout'); }); connection.on('disconnected', function() { console.error('APNS: Lost connection'); }); connection.on('socketError', console.log); 

同样重要的是,您需要确保您的脚本在正在处理asynchronous请求时处于运行状态。 大多数情况下,当你构build一个更大,更大的服务时,你将最终得到一个这样的事件循环,像ActionHero,ExpressJS,Sails等框架将为你做这个。

同时,你可以用这个超级粗糙的循环来确认它,它只是强制进程保持运行,直到你按下CTRL + C

 setInterval(function() { console.log('Waiting for events...'); }, 5000);