Sunday, 12 March 2017

Sending full fledged iOS notifications with node APN

I am trying to replace the old notification system with the one based on as single key. I used the example at Eladnava using a node script to deliver the notifications. After a few problems mostly due to the version of node, I managed to deliver the notification in the form:

{"meditator":"33","latitude":"41.718937","longitude":"12.511676","starting":1,"total":0,"aps":{"badge":0,"alert":"toAnyone"}}

with no mention whatever to the content_available setting useful to handle the notification without triggering alerts. Yet when I check the value on:

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) 

or func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Swift.Void) what I receive is just a small part of it:

[AnyHashable("id"): 123, AnyHashable("aps"): {
    alert = toAnyone;
    badge = 0;
    sound = "ping.aiff";
}]

and the notification is always presented as an alert when the app is in background. How may I produce the same result I had in the old version of the notifications protocol? In particular what is left of the loc-key and lic-args previously used to send data? Here is the node function I am using:

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: 'GHIJK', // 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();

// Specify your iOS app's Bundle ID (accessible within the project editor)
notification.topic = app;
notification.expiry = Math.floor(Date.now() / 1000) + 3600;

// Set app badge indicator
console.log("badge="+badge+"|");
notification.badge = badge;
notification.phase = phase;
//
// Play ping.aiff sound when the notification is received

//notification.sound = 'ping.aiff';
// Display the following message (the actual notification text, supports emoji)
notification.alert = message;
//
// // Send any extra payload data with the notification which will be accessible to your app in didReceiveRemoteNotification
notification.payload = JSON.parse(params);
//{"meditator":"33","latitude":"41.718937","longitude":"12.511676","starting":1,"total":0}
console.log(params);
notification.content_available=content_available;
//
// // Actually send the notification
const util = require('util');
console.log("notification:"+JSON.stringify(notification));
apnProvider.send(notification, deviceToken).then(function(result) {
    console.log(util.inspect(result, false, null))
    process.exit(0);
//  console.log(result);
});



via Fabrizio Bartolomucci

No comments:

Post a Comment