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