发送多个iPhone通知

发送多个iPhone通知,iphone,notifications,apple-push-notifications,Iphone,Notifications,Apple Push Notifications,当我需要发送一个通知时,我的代码工作正常,但每次当我需要发送多个通知时,它只发送第一个通知。代码如下: <?php $device_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $apnsHost = 'gateway.sandbox.push.apple.com'; $apnsPort = 2195; $apnsCert = 'apns-dev.pem'; $streamContext = st

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

<?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);
}?>

我做错了什么

Thx提前,
Mladjo

在黑暗中拍摄。查看您的for循环


您似乎已打开连接并推送消息。。。但这种联系是否会自我封闭?您是否需要为每次推送启动一个新连接,从而在重新启动另一个连接之前,有必要在while循环结束时关闭第一个连接?

这里是在黑暗中拍摄。查看您的for循环


您似乎已打开连接并推送消息。。。但这种联系是否会自我封闭?您是否需要为每次推送启动一个新连接,从而有必要在while循环结束时关闭第一个连接,然后再重新启动另一个连接?

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

$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);
}?>
$apns=stream\u socket\u client('ssl://'.$apnsHost'.:'.$apnsPort,$error,$errorString,60,stream\u client\u CONNECT,$streamContext);
对于($i=0;$i)

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

您应该只打开一次到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);
}?>
$apns=stream\u socket\u client('ssl://'.$apnsHost'.:'.$apnsPort,$error,$errorString,60,stream\u client\u CONNECT,$streamContext);
对于($i=0;$i)

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

请查看以下文档:

它说,应使用TCP/IP Nagle算法在一次传输中发送多个通知。您可以在此处找到Nagle算法:

因此,我认为创建消息的代码应该如下所示:

// 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连接并发送消息….

查看以下文档:

它说,应使用TCP/IP Nagle算法在一次传输中发送多个通知。您可以在此处找到Nagle算法:

因此,我认为创建消息的代码应该如下所示:

// 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连接并发送消息…

我马上就注意到了。这是一个复制和粘贴错误。在我的代码中,我只打开了一次连接。这不是我问题的解决方案。您是否尝试使用以下代码生成消息:$apns_Message=chr(0)。pack('n',32)。pack('H*,$device_token)。pack('n',strlen($payload)).$payload;另外,您是否将所有推送通知发送到同一台设备?如果是,则设备上只会显示一个。我的消息是用该代码生成的。我将所有推送通知发送到同一个iPod touch。通过此循环,我接收到的是2而不是5。即使我尝试使用不同的令牌,它也只发送到第一个。也许您应该尝试一下通过在脚本fclose($apns)末尾正确关闭套接字来刷新套接字@YannBiancheri我能有人把这段代码翻译成c吗?我马上就注意到了。这是复制和粘贴错误。在我的代码中,我只打开了一次连接。这不是我问题的解决方案。你有没有试过用这段代码构建你的消息:$apns_message=chr(0)。pack('n',32)。pack('H*,$devicetoken)。pack('n',strlen($payload)).$payload;另外,您是否将所有推送通知发送到同一台设备?如果是,则设备上只会显示一个。我的消息是用该代码生成的。我将所有推送通知发送到同一个iPod touch。通过此循环,我接收到的是2而不是5。即使我尝试使用不同的令牌,它也只发送到第一个。也许您应该尝试一下要通过在脚本fclose($apns)末尾正确关闭套接字来刷新套接字;@YannBiancheri有人能将此代码翻译成c吗#