日期:2014-05-16 浏览次数:20420 次
var apn=require("apn")
var token = '60c3b0b7 b6c464f0 e86bf6f0 ac1fc3d3 26701bae fae89183 4c2d80d9 51811965'; //长度为64的设备Token
var options = { "gateway": "gateway.sandbox.push.apple.com"
,"cert": "./pro_mic_cer.pem",
"key": "./pro_mic_key.pem"
},
apnConnection = new apn.Connection(options),
device = new apn.Device(token),
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 = "\uD83D\uDCE7 \u2709 You have a new message";
note.payload = {'messageFrom': 'Caroline'};
apnConnection.pushNotification(note, device);
apnConnection.on('connected',function() {
console.log("Connected");
});
apnConnection.on('transmitted',function(notification, device) {
console.log("Notificationtransmitted to:" + device.token.toString('hex'));
});
apnConnection.on('transmissionError',function(errCode, notification, device) {
console.error("Notificationcaused error: " + errCode + " for device ", device,notification);
});
apnConnection.on('timeout',function () {
console.log("ConnectionTimeout");
});
apnConnection.on('disconnected',function() {
console.log("Disconnectedfrom APNS");
});
apnConnection.on('socketError',console.error);
D:\workspace\workspace\testApns>node app.js
Notificationcaused error: 513 for device { token: <Buffer 60 c3 b0 b7 b6 c4 64
f0 e8 6b f6 f0 ac 1f c3 d3 26 70 1b ae fa e8 91 83 4c 2d 80 d9 51 81 19 65> } {
encoding: 'utf8',
payload:
{ messageFrom: 'Caroline',
aps:
{ badge: 3,
sound: 'ping.aiff',
alert: 'You have a new message' } },
expiry: 1389887110,
identifier: 0,
priority: 10,
retryLimit: -1,
device: undefined,
alert: 'You have a new message',
badge: 3,
sound: 'ping.aiff',
newsstandAvailable: undefined,
contentAvailable: undefined,
mdm: undefined,
compiled: true,
truncateAtWordEnd: false,
urlArgs: undefined,
compiledPayload: '{"messageFrom":"Caroline","aps":{"badge":3,"sound":"ping.aif
f","alert":"You have a new message"}}' }