发送多个 iPhone 通知

发布于 2024-08-08 20:34:04 字数 894 浏览 5 评论 0原文

当我需要发送一个通知时,我的代码工作正常,但每次当我需要发送多个通知时,它只发送第一个通知。这是代码:

<?php
$device_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

$payload['aps'] = array('alert' => 'some notification', 'badge' => 0, 'sound' => 'none');
$payload = json_encode($payload);

for($i=0; $i<5; $i++)
{
    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;

    fwrite($apns, $apnsMessage);
}?>

我做错了什么?

提前谢谢, 姆拉乔

My code works ok when I need to send one notification, but each time when I need to send more than one, it only sends the first one. Here is the code:

<?php
$device_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

$payload['aps'] = array('alert' => 'some notification', 'badge' => 0, 'sound' => 'none');
$payload = json_encode($payload);

for($i=0; $i<5; $i++)
{
    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;

    fwrite($apns, $apnsMessage);
}?>

What am I doing wrong?

Thx in advance,
Mladjo

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

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

发布评论

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

评论(3

千纸鹤带着心事 2024-08-15 20:34:04

您应该只打开一次与 apns 的连接。现在你正在循环中打开它,这是错误的。我还使用稍微不同的方案来构建我的消息。您应该这样做:

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
for($i=0; $i<5; $i++)
{
        $apns_message = chr(0).pack('n', 32).pack('H*', $device_token).pack('n', strlen($payload)).$payload;

        fwrite($apns, $apnsMessage);
}?>

另请注意,苹果建议使用相同的连接来发送所有推送通知,因此您不应在每次有推送通知要发送时都进行连接。

You should open the connection to apns only once. Right now you are opening it in the loop which is wrong. I'm also using a slightly different scheme to build my messages. You should instead do it in this way:

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
for($i=0; $i<5; $i++)
{
        $apns_message = chr(0).pack('n', 32).pack('H*', $device_token).pack('n', strlen($payload)).$payload;

        fwrite($apns, $apnsMessage);
}?>

Also note that apple recommends using the same connection to send all your push notifications so you shouldn't connect every time you have a push notification to send.

空城旧梦 2024-08-15 20:34:04

看看下面的文档:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3

它表示应使用 TCP/IP Nagle 算法在一次传输中发送多个通知。您可以在这里了解 Nagle 算法是什么:
http://en.wikipedia.org/wiki/Nagle%27s_algorithm

所以我相信创建消息的代码应如下所示:

// Create the payload body
$body['aps'] = array(
'alert' => "My App Message",
'badge' => 1);

// Encode the payload as JSON
$payload = json_encode($body);

// Loop through the token file and create the message
$msg = "";
$token_file = fopen("mytokens.txt","r");
if ($token_file) {
    while ($line = fgets($token_file)) {
        if (preg_match("/,/",$line)) {
            list ($deviceToken,$active) = explode (",",$line);
            if (strlen($deviceToken) == 64 && intval($active) == 1) {
                // Build the binary notification
                $msg .= chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
            }
        }
    }
    fclose ($token_file);
}


if ($msg == "") {
    echo "No phone registered for push notification";
    exit;
}

现在打开 TCP 连接并发送消息......

Have a look at the following document:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3

It says that multiple notifications should be sent in a single transmission using the TCP/IP Nagle algorithm. You can find out what the Nagle algorithm is here:
http://en.wikipedia.org/wiki/Nagle%27s_algorithm

so I believe the code to create the message should look like:

// Create the payload body
$body['aps'] = array(
'alert' => "My App Message",
'badge' => 1);

// Encode the payload as JSON
$payload = json_encode($body);

// Loop through the token file and create the message
$msg = "";
$token_file = fopen("mytokens.txt","r");
if ($token_file) {
    while ($line = fgets($token_file)) {
        if (preg_match("/,/",$line)) {
            list ($deviceToken,$active) = explode (",",$line);
            if (strlen($deviceToken) == 64 && intval($active) == 1) {
                // Build the binary notification
                $msg .= chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
            }
        }
    }
    fclose ($token_file);
}


if ($msg == "") {
    echo "No phone registered for push notification";
    exit;
}

And now open the TCP connection and send the Message....

挽心 2024-08-15 20:34:04

在这里黑暗中拍摄。看看你的 for 循环。

看起来您打开了连接并推送了消息...但是该连接会自行关闭吗?您是否需要为每次推送启动一个新连接,从而需要在 while 循环结束时关闭第一个连接,然后才能重新启动另一个连接?

Taking a shot in the dark here. Looking at your for loop.

It looks like you open the connection and push the message... but does that connection close itself? Do you need to initiate a new connection for each push thereby making it necessary to close the first connection at the end of the while loop before re-initiating another?

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