I am trying to send a rich notification to iOS by prebuilding the payload and then sending it in the notification. Yet for some reason no notification arrives and what is even stranger when I print the notification before sending it, it is shown as an empty array, notwithstanding I correctly assigned values to it. So it sems more of a JS problem than an APN one. This is the code I developed on the basis of: https://github.com/node-apn/node-apn/blob/master/doc/notification.markdown#convenience-setters
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;
// // 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);
});
May someone tell me why that notification dictionary ends as empty?
via Fabrizio Bartolomucci
No comments:
Post a Comment