我的应用有一个推送通知 token 列表(大约 10k),我不跟踪事件/删除 token 的状态。当一个新用户被添加到系统中时,它会被添加到它的推送 token 中
(一个设备可以持有一个推送通知 token ,以防 token 发生变化——在我的应用程序生命周期的最后 6 个月内没有发生——它会被新 token 修改)
为了向单个用户发送推送通知,我正在使用以下 php 脚本
<?php
// connecting to sql
sql_connect();
// get user token from sql
$token = sql_gettoken();
// Put your device token here (without spaces):
$deviceToken = $token;
// Put your private key's passphrase here:
$passphrase = 'my_passphrase';
// Put your alert message here:
if ($_GET["message"]) $message = $_GET["message"];
else $message = 'This is a default message in case no message is defined';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'production_cerfiticate.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.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);
?>
我相信如果我保持 $fp(与 APNS 服务器的连接)打开,我可以在两者之间发送多个推送通知(我不确定?)。但我已经读到,如果 token 离线/删除应用程序,那么连接将由苹果端关闭,这意味着推送通知周期将失败。
将这个单一推送通知脚本转换为广播通知有什么技巧吗?我不鼓励测试这个,因为第一次测试将是现场直播,我只有一次机会。
编辑:我错过了最重要的部分,如果我只是为所有设备 token 循环调用此 php 脚本,这意味着它将尝试为已删除的应用程序和事件调用此脚本, Apple 会阻止/停用/禁止我的证书或我发送推送通知的能力吗?
- 对于其他寻求广播的人来说,Apple 似乎对推送通知没有任何限制。
干杯
最佳答案
只要连接保持打开状态,您就应该保持连接打开并使用同一连接发送通知。这就是 Apple 所说的你应该做的。
如果应用程序从设备上卸载,则在发送到该设备时连接不会关闭。仅当 token 对当前环境永远无效(这通常发生在您尝试将沙盒设备 token 发送到生产环境或反之亦然)或消息有其他错误(例如负载太长)时,它才会关闭).
当您向卸载了该应用程序的设备发送通知时,APNS 会发现该设备卸载了该应用程序,稍后,当您调用反馈服务时,您将获得该设备 token 。 Apple 要求您定期调用反馈服务,并从您的数据库中删除它返回的设备。如果您不使用反馈服务并不断向多次卸载该应用程序的设备发送通知,Apple 可能会阻止您。
我建议您阅读 Troubleshooting Push Notifications ,尤其是推送通知吞吐量和错误检查部分。这非常有帮助。
关于php - 通过 PHP 广播推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15850147/