ajay
ajay

Reputation: 9680

Node.js node-apn module for Apple Push Notification stopped working after moving to Amazon EC2 instance running Ubuntu 12.04

I am using node-apn module to avail of APNS (Apple Push Notification Service) to push notification to multiple devices. The local environment is Mac OS X Lion 10.7 and the code works perfectly fine when run at local server.Here's the snippet:

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')); 

I moved the exact same code to the Amazon EC2 instance running Ubuntu 12.04 and it doesn't work there. None of the events above which I handle gets triggered. I checked certificate and key files and paths by printing out the options and apnsConnection objects and there seems no problem there. I don't know what the issue is. Any help will be greatly appreciated.

Upvotes: 2

Views: 3966

Answers (1)

Joel Pettersson
Joel Pettersson

Reputation: 136

Check your node --version s. On my Mac, homebrew installed v0.10.12 but on the server I was running v0.11.8-pre (by mistake). Apparently the present node-apn doesn't work under the unstable.

I had this same extremely frustrating issue and hunted among firewalls/OpenSSH stuff until I realized the APNS connection still worked through PHP. The Ray Wenderlich script is a quick way to check if the problem is with node or elsewhere.

Upvotes: 1

Related Questions