我正在尝试实施iOS推送通知。我的PHP版本停止了工作,我还没有能够让它再次运行。但是,我有一个使用Apple的新Auth Key完美运行的node.js脚本。我可以使用以下方法从PHP调用它:
chdir("../apns");
exec("node app.js &", $output);
但是,我希望能够将deviceToken和消息传递给它。有没有办法将参数传递给脚本?
这是我尝试运行的脚本(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)
});
答案 0 :(得分:11)
您可以传递参数,就像将其传递给任何其他脚本一样。
node index.js param1 param2 paramN
您可以通过process.argv
访问参数process.argv属性返回包含命令行的数组 启动Node.js进程时传递的参数。首先 元素将是process.execPath。如果访问,请参阅process.argv0 需要argv [0]的原始值。第二个元素是 正在执行的JavaScript文件的路径。剩下的要素 将是任何其他命令行参数。
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' ]
*/
您可以使用minimist为您解析参数:
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
答案 1 :(得分:0)
经过大量工作,我有一个解决方案。我正在使用本地Web UI向Arduino发送数据或从Arduino接收数据 使用AJAX,php脚本为:
MINIO_STORAGE_USE_HTTPS=False
Node Js脚本是:
<?php
/**
* Remember to go to Device Manager> Ports (COM & LPT)>Arduino XXX (COMXX)>right
* click>Properties>
* Port Settings>Advanced>uncheck "use FIFO buffers ........."
* In other hand, remeber that the Tx speed has to be the same in writeandread.js as in
* Arduino sketch and in the COM
* properties in Device manager, I selected 115200 b/s.
*
*/
$puerto = escapeshellarg("COM3");
$dato = escapeshellarg("<4,2,20>");
exec("node C:/xampp/htdocs/DisenoWEBTerminados/BatteryTester/Scripts/writeandread.js {$puerto} {$dato} 2>&1", $output);
$myJSON = $output;
$pepe = implode($myJSON);
echo $pepe;
echo "<script>console.log('Rx from Arduino ".$pepe."')</script>";
?>
使用此脚本,您可以从Arduino发送和接收数据,并将其传递给客户端的AJAX脚本,并执行所需的操作。现在,我向php第一个脚本添加了一个脚本,以编程方式检测Arduino连接到的COM端口。
享受。