I'm trying to implement iOS push notifications. My PHP version stopped working and I haven't been able to get it working again. However, I have a node.js script that works perfectly, using Apple's new Auth Key. I am able to call that from PHP using:
chdir("../apns");
exec("node app.js &", $output);
However, I would like to be able to pass the deviceToken and message to it. Is there any way to pass parameters to the script?
Here's the script I'm trying to run (app.js):
var apn = require('apn');
var apnProvider = new apn.Provider({
token: {
key: 'apns.p8', // Path to the key p8 file
keyId: '<my key id>', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key)
teamId: '<my team id>', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)
},
production: false // Set to true if sending a notification to a production iOS app
});
var deviceToken = '<my device token>';
var notification = new apn.Notification();
notification.topic = '<my app>';
notification.expiry = Math.floor(Date.now() / 1000) + 3600;
notification.badge = 3;
notification.sound = 'ping.aiff';
notification.alert = 'This is a test notification \u270C';
notification.payload = {id: 123};
apnProvider.send(notification, deviceToken).then(function(result) {
console.log(result);
process.exit(0)
});
You can pass parameters as you would pass it to any other script.
node index.js param1 param2 paramN
You can access the arguments through process.argv
The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched. The first element will be process.execPath. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command line arguments.
exec("node app.js --token=my-token --mesage=\"my message\" &", $output);
app.js
console.log(process.argv);
/*
Output:
[ '/usr/local/bin/node',
'/your/path/app.js',
'--token=my-token',
'--mesage=my message' ]
*/
You can use minimist to parse the arguments for you:
const argv = require('minimist')(process.argv.slice(2));
console.log(argv);
/*
Output
{
_: [],
token: 'my-token',
mesage: 'my message'
}
*/
console.log(argv.token) //my-token
console.log(argv.message) //my-message
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With