Sending a preformed notification

78 views
Skip to first unread message

Fabrizio Bartolomucci

unread,
Mar 15, 2017, 10:41:45 AM3/15/17
to node-apn
I am tryng to deliver the same format of notifications I had in the past implementation by assigning the paylod with the full dictionary to the notification. My objective is in particular to assign the content_available variable to optional delive silent notifications and some random fields I use in the app: the strange issue is that when I try to print the notification, it comed out as an empty array, while if I print its fields they have values. Whatever the reason, no notification is sent, anyway. As a matter of fact I am not sure the correct why of assigning values to apn is the one I use, but I may not think of an alternative way. This is my code:

process.argv.forEach(function (val, index, array) {
    switch(index){
        case 2:
            deviceToken=val;
            console.log( "token:"+deviceToken);
        break;
    case 3:
        message=val;
        console.log("3: message:"+message);
        break;
    case 4:
        phase=val;
        console.log( "4: phase:"+phase);
        break;
    case 5:
        params=val;
        console.log( "5: params:"+params);
        break;
    case 6:
        app=val;
        console.log("6: app:"+app);
        break;
    case 7:
        badge=Number(val);
        console.log("7: badge:"+badge);
        break;
    case 8:
        content_available=val;
        console.log("8: content_available:"+content_available);
        break;
    case 9:
        category=val;
        console.log("9: category:"+category);
        break;
    case 10:
        distribution=(val==1);
        console.log("10: distribution:"+distribution);
        break;
    default:
        console.log(index+": "+val);
    }
})
var apn = require('apn');
console.log("starting apnProvider distribution="+distribution);
// Set up apn with the APNs Auth Key
var apnProvider = new apn.Provider({  
    token: {
        key: './apns.p8', // Path to the key p8 file
        keyId: 'ABCDEF', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key)
        teamId: 'GHIKK', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)
    },
    production: distribution // Set to true if sending a notification to a production iOS app
});
console.log("completed apnProvider");
// Prepare a new notification
var notification = new apn.Notification();
var body = new Object();
if (content_available==1){
// Create the payload body
        body['aps'] = {
            'content-available': 1,
            'sound':''
        };

        body['loc-key'] = message;
        body['loc-args'] = JSON.parse(params);
        body['phase'] = phase;
} 
else if (message!=""){
    messageArray={
        'loc-key': message,
        'loc-args': params
        };
// Create the payload body
    body['aps'] = {
        'alert': messageArray,
        'sound': 'default'
        };   
}
body['aps']['category']=category;
body['aps']['badge']=badge;
console.log("body:"+JSON.stringify(body));
// Specify your iOS app's Bundle ID (accessible within the project editor)
notification.topic = app;

notification.payload = body; 
console.log("notification.payload:"+JSON.stringify(notification.payload)); //-->> prints {"aps":{"alert":{"loc-key":"ciao Fabrizio bello","loc-args":"{\"meditator\":33,\"latitude\":41.718937,\"longitude\":12.511676,\"starting\":1,\"total\":0}"},"sound":"default","category":"0","badge":3}}
// // Actually send the notification const util = require('util'); console.log("notification:"+JSON.stringify(notification)); //-->>prints {} apnProvider.send(notification, deviceToken).then(function(result) { console.log(util.inspect(result, false, null)) process.exit(0); // console.log(result); 
})


Reply all
Reply to author
Forward
0 new messages