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オブジェクトを出力して、証明書とキーファイルとパスを確認しましたが、問題はないようです。何が問題なのかわからない。どんな助けでも大歓迎です。