我正在使用 node-apn 模块来利用 APNS(Apple Push Notification Service)将通知推送到多个设备。本地环境是 Mac OS X Lion 10.7,代码在本地服务器上运行时运行良好。这是代码片段:
var apns = require('apn');
var options = {
cert: __dirname + '/PushDevCertKey.pem',
certData: null,
key: __dirname + '/PushDevCertKey.pem',
keyData: null,
passphrase: 'admin',
ca: null,
pfx: null,
pfxData: null,
gateway: 'gateway.sandbox.push.apple.com',
port: 2195,
rejectUnauthorized: true,
enhanced: true,
errorCallback: apnErrorCallback,
cacheLength: 100,
autoAdjustCache: true,
connectionTimeout: 0
}
var apnsConnection = new apns.Connection(options);
var note = new apns.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600;
note.badge = 1;
note.sound = 'ping.aiff';
note.alert = 'you have a new message';
note.payload = {'rid': roomId};
apnsConnection.pushNotification(note, deviceTokenArray);
// i handle these events to confirm the notification gets
// transmitted to the APN server or find error if any
function log(type) {
return function() {
console.log(type, arguments);
}
}
apnsConnection.on('error', log('error'));
apnsConnection.on('transmitted', log('transmitted'));
apnsConnection.on('timeout', log('timeout'));
apnsConnection.on('connected', log('connected'));
apnsConnection.on('disconnected', log('disconnected'));
apnsConnection.on('socketError', log('socketError'));
apnsConnection.on('transmissionError', log('transmissionError'));
apnsConnection.on('cacheTooSmall', log('cacheTooSmall'));
我将完全相同的代码移至运行 Ubuntu 12.04 的 Amazon EC2 实例,但它在那里不起作用。我处理的上述事件都没有被触发。我通过打印选项和 apnsConnection 对象检查了证书和密钥文件和路径,那里似乎没有问题。我不知道问题是什么。任何帮助将不胜感激。