我有这个php文件,当我用它发送到我的设备时,它没问题 我收到通知没有任何问题 现在我有超过设备令牌,我想修改php文件,使循环发送到所有设备
<?php
// Put your device token here (without spaces):
$deviceToken = '';
// Put your private key's passphrase here:
$passphrase = '';
// Put your alert message here:
$message = '';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
答案 0 :(得分:0)
从我所看到的情况来看,你需要将整个脚本的下半部分放在一个循环中并遍历每个存储在数组中的设备。可能有一种更好的方法,取决于你使用的系统实际工作的方式,但如果我没有犯任何错误,下面应该达到你想要的,这很可能是因为我可以'测试它。
<?php
$message = ''; //Put Message Here
$devices = Array();
$devices[0] = Array();
$devices[0]["deviceToken"] = ''; //Put First DeviceToken Here
$devices[0]["passphrase"] = ''; //Put First Passphrase Here
$devices[1] = Array();
$devices[1]["deviceToken"] = ''; //Put Second DeviceToken Here
$devices[1]["passphrase"] = ''; //Put Second Passphrase Here
//Copy and paste the above 3 lines as desired, adding 1 to the number $devices[<NUMBER>] for each additional device
//Make sure to put their specific information in each line.
//-------------------------------------------------------
foreach($devices as $device){
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $device["passphrase"]);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $devices["deviceToken"]) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
}
?>