Search code examples
node.jsapple-push-notificationsapn

Node js apple push notification with sending custom data


Now push notification works properly, but I don't know how to send custom data with push notification.

My node js library for push notification is 'apn'.

Code for push notification :

    var apns = require('apn');

    /* Data to be send with push notification */
    var ntfnDetails = {};
    ntfnDetails = data.ntfnDetails;


    var options = {
        cert: 'FitCert.pem',                 /* Certificate file path */
        certData: null,                   /* String or Buffer containing certificate data, if supplied uses this instead of cert file path */
        key:  'FITHUDLEKEY.pem',                  /* Key file path */
        keyData: null,                    /* String or Buffer containing key data, as certData */
        passphrase: 'titech!@#',                 /* A passphrase for the Key file */
        ca: null,                         /* String or Buffer of CA data to use for the TLS connection */
        gateway: 'gateway.push.apple.com',/* gateway address */
        port: 2195,                       /* gateway port */
        enhanced: true,                   /* enable enhanced format */
        errorCallback: undefined,         /* Callback when error occurs function(err,notification) */
        cacheLength: 100                  /* Number of notifications to cache for error purposes */
    };

    var apnsConnection = new apns.Connection(options);

    var myDevice = data.device_id;

    var note = new apns.Notification();

    note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
    note.badge = 3;
    note.alert = 'Hellooo';
    note.payload = {'messageFrom': 'Caroline'};

    apnsConnection.pushNotification(note, myDevice);

In this I want to send 'ntfnDetails' variable with this push notification.

Please help me to find a solution...

Thanks in advance.


Solution

  • I think you can set it in payload

    note.payload = {'messageFrom': 'Caroline', 'ntfnDetails' : ntfnDetails };
    

    Check this apple-push-notification-with-sending-custom-data