Mac系统上用Node做APNS

1、安装Node,下载地址:http://nodejs.org

2、更新npm,终端命令:sudo npm update npm -g

3、安装apn,终端命令:npm install apn

4、导出证书并生成pem文件

        参照:http://www.tuicool.com/articles/fABVZb

5、Node源码:

var apns = require('apn');
var options = {
    cert : 'cert.pem',                 /* Certificate file path */
    key :  'key.pem',                  /* Key file path */
    gateway : 'gateway.sandbox.push.apple.com',/* gateway address */
    port: 2195,                       /* gateway port */
    errorCallback: errorHappened ,         /* Callback when error occurs function(err,notification) */
};
function errorHappened(err, notification){
    console.log("err " + err);
}
var apnsConnection = new apns.Connection( options );
var token = "<device token>";
var myDevice = new apns.Device( token );
var note = new apns.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note. badge = 11;
note. sound = "ping.aiff";
note. alert = "You have a new message";
note. payload = {'messageFrom': 'Caroline'};
note.device = myDevice;

apnsConnection.sendNotification (note);

“<device token>”为接收push用的设备的DeviceToken字符串,不带空格及前后尖括号。



参考资料:http://www.tuicool.com/articles/fABVZb

posted @ 2015-01-26 22:30  脱离语言  阅读(207)  评论(0编辑  收藏  举报