iPhone推送通知服务问题

发布于 2024-10-27 18:44:52 字数 1175 浏览 3 评论 0 原文

我在我的应用程序和 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2024-11-03 18:44:52

不确定是否有限制,但我确信它不会低,因为我过去曾一次成功地向数千台设备发送通知(尽管目前我一次向它们发送大约 250-500 个) )。

尝试替换此:

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

使用此:

$apnsMessage = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload;

另外,您可能有兴趣尝试一下: 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:

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

With this:

$apnsMessage = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload;

Also, you might be interested in trying this out: http://code.google.com/p/apns-php/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文