我在我的应用程序和 PHP 服务器中使用推送通知,该服务器使用令牌管理数据库并将有效负载发送到 Apple 服务器。向少量设备(我尝试过 2 个)发送消息效果很好,但是当我想向整个数据库(超过 20,000 个设备)发送消息时,它不起作用。与苹果服务器的连接已建立(我没有收到连接错误),但我拥有的两个设备(也在数据库中)没有收到消息。
PHP 代码是:
$apnsHost = 'gateway.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'path/to/my/certificate.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
while($row = mysql_fetch_array($result))
{
$deviceToken=$row['token'];
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
if ($error==0)
{
echo "Notification was sent successfully to ".$row['token']."<br/>";
}
else
{
echo "Notification failure to ".$row['token']."<br/>";
}
}
socket_close($apns);
fclose($apns);
结果,我收到数据库中所有记录的“通知已成功发送”,但它似乎没有发送消息,因为我在我的设备上没有看到它。当我使用相同的代码向 2 个令牌发送消息时,效果很好。可能是什么问题?通过一个连接可以发送到的设备数量是否有上限?
I am using Push Notification in my app and a PHP server that manages database with tokens and sending payload to Apple servers. Sending messages to a small number of devices (I have tried with 2) works well but when I want to send a message to entire database ( over 20.000 devices ) it doesn't work. Connection with Apple server is made ( i get no errors of connection ) but the two devices I have ( that are also in database ) do not received the message.
The PHP code is:
$apnsHost = 'gateway.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'path/to/my/certificate.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
while($row = mysql_fetch_array($result))
{
$deviceToken=$row['token'];
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
if ($error==0)
{
echo "Notification was sent successfully to ".$row['token']."<br/>";
}
else
{
echo "Notification failure to ".$row['token']."<br/>";
}
}
socket_close($apns);
fclose($apns);
As a result I am getting "Notification was sent successfully" for all the records in database but it seems like it doesn't send the message because I don't see it on my devices. When I am using the same code to send message to 2 tokens it works well. What could be the problem? Is there an upper limit for number of devices I can send to with one connection?
发布评论
评论(1)
不确定是否有限制,但我确信它不会低,因为我过去曾一次成功地向数千台设备发送通知(尽管目前我一次向它们发送大约 250-500 个) )。
尝试替换此:
使用此:
另外,您可能有兴趣尝试一下: http:// code.google.com/p/apns-php/
Not sure if there is a limit, but I'm sure it won't be low as I have successfully sent notifications to thousands of devices at a time in the past (although currently I'm shooting them about 250-500 at a time).
Try replacing this:
With this:
Also, you might be interested in trying this out: http://code.google.com/p/apns-php/