php - 同时发送 5000 个推送通知,保持与苹果的连接?

标签 php ios iphone push-notification apple-push-notifications

现在我正在使用下面的代码来发送我的推送通知

function applePush($deviceToken,$sound,$message,$object,$thread = 0)
{
$passphrase = 'Secret';


        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'secret.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);

        //Customizable sounds
        if ($sound == 0) { $sound = 'default'; }
        else {$sound = '/' . $sound . '.wav'; }         

        // Create the payload body
        if ($thread > 0)
            {

                    $body['aps'] = array(
                        'alert' => $message,
                        'sound' => $sound,
                        'obj' => $object,
                        't' => $thread,
                        );

            }
        else
            {
                    $body['aps'] = array(
                        'alert' => $message,
                        'sound' => $sound,
                        'obj' => $object
                        );

            }   


        // 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));


        // Close the connection to the server
        fclose($fp);

}

我实际上在做的是: 1) 从 SQL 数据库中提取 5000 个(并且还在增加)设备 token 。
2) 使用 PHP 循环,对于每个设备 token ,我运行上面列出的 applePush 函数

现在一切正常,每个人都得到了苹果的插入。现在更重要的是,我自己的个人 iphone 是我 SQL 数据库中的第一台设备,我会立即收到推送通知。 但是,我刚买了一部新的 iphone,我现在是数据库中的最后一台设备,我注意到现在我需要将近 30 分钟才能收到通知。我认为这与与苹果建立所有这些连接的物理时间有关,但是当它到达第 5000 个设备 ID 时,已经过去了 30 分钟。

这让我开始阅读,Apple 似乎实际上建议保持与 APNS 的连接打开,而不是不断打开和关闭连接。

所以我写信是为了征求大家的意见,我这样做是不是错了?如果我是,我该如何修改我的代码以使其符合苹果政策并使其更快/更高效

谢谢!

最佳答案

不要打开和关闭连接 5000 次,这是非常昂贵且 Not Acceptable 。

问题与 Android 不同,Apple 服务器不会在播放负载的同时获取设备 token 数组。

更好的方法是遍历数据库并返回一个数组。

一旦连接打开,这个数组就会在循环中使用。

所以你会得到这样的东西:

//Apple Push Notifcations
$apn = new Apple_Push_Notifications();

//Get Device tokens into an array
$deviceTokens = array();
$deviceTokens = $apn->GetDeviceTokens();

//Push the notifications
if(count($deviceTokens) > 0){
    $apn->applePush($deviceTokens,$sound,$message,$object,$thread);
}

//In applePush loop through all the tokens and submit
function applePush($deviceTokens,$sound,$message,$object,$thread = 0)
{
    ....
    // 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);
    ....
    ....
    // Encode the payload as JSON
    $payload = json_encode($body);

    //Send the Push to each token the close connection
    foreach ($deviceTokens as $deviceToken) {
        // 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));
    }

    // Close the connection to the server
    fclose($fp);
}

关于php - 同时发送 5000 个推送通知,保持与苹果的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763514/

相关文章:

javascript - jQuery - 通过 Id 获取动态 (*) 元素并放入数组

iphone - 使用自定义动画删除自定义 UIStoryboardSegue

ios - Xcode - 在 View Controller 之间传递数据,然后转换为整数

ios - 如何通过自己的应用程序在 iPhone 中启用/禁用振动?

php - Codeigniter - 事务测试模式不起作用

php - 如何防止在表中创建重复的 IP?

php - 导入带有 2 个合并列的 sql

iphone - UIWebView 离线渲染数据

ios - 停止 AudioUnit 录制后出现错误

iphone - 是否可以通过Gradle构建iphone项目?