批量/多个 iOS 推送通知代码 - 适用于 2 台设备,但不适用于 100 台设备 [英] Batch / multiple iOS Push Notification code - works for 2 devices, but not for 100

查看:49
本文介绍了批量/多个 iOS 推送通知代码 - 适用于 2 台设备,但不适用于 100 台设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我发送到的设备数量为 2,以下代码可以正常工作 - 即它们都收到推送通知.但如果我将该限制提高到 100,则不会收到推送通知.

The following code works fine if the number of devices I send to is 2 - i.e., they both receive the push notifications. But if I raise that limit to 100, no push notifications are received.

我已经阅读了这一点,看起来我正在正确发送批处理通知(即,通过单个连接发送多个请求);连接的超时设置很好并且很高(60 秒);代码的输出看起来都井井有条;apache 错误日志中没有任何内容,所以我看不出问题出在哪里.

I have read up on this and it looks like I am sending the batch notifications correctly (i.e., multiple requests via a single connection); the timeout of the connection is set nice and high (60 seconds); the output of the code all looks in order; nothing in the apache error log, so I don't see where the problem is.

我的客户真的被黑了.有人可以帮忙吗??

My customer's getting really hacked off. Can anyone help??

函数 sendIosPushes() {

function sendIosPushes() {

$payload['aps'] = array('alert' => pushMessage, 'badge' => badgeNumber, 'sound' => 'default');
$payload = json_encode($payload);

//$statement = "SELECT * FROM push_ios WHERE device_token = 'device token of my iphone" OR device_token = 'device token of my ipod'; //works selecting my two devices from table
$statement = "SELECT * FROM push_ios"; //doesn't work when selecting all devices from table


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

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

$numDevices = 0;
$numRequestsSent = 0;
$result = mysql_query($statement);
while ($row = mysql_fetch_assoc($result)) {
    $numDevices++;
    try {
        $deviceToken = $row['device_token'];

        //$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', $deviceToken) . chr(0) . chr(strlen($payload)) . $payload;
        $apnsMessage = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; //from http://stackoverflow.com/questions/1642411/sending-multiple-iphone-notifications

        $fwriteRes = fwrite($apns, $apnsMessage, strlen($apnsMessage));
        echo "Push sent to " . $deviceToken . "<br />\n";

        $numRequestsSent++;
    }
    catch(Exception $e) {
        echo "1. Exception: " . $e->getMessage() . "<br />\n";
    }
}

fclose($apns);


if ($error != 0 || (isset($errorString) && strlen($errorString) > 0 )) {
    echo "ERROR: " . $error . " - ". $errorString . "<br />\n";
}

return $numDevices . " iOS devices. " . $numRequestsSent . " requests sent.";

}

推荐答案

您的数据库中可能有一些无效的设备令牌.

You probably have some invalid device tokens in your DB.

如果设备令牌无效,如果您使用较新的二进制格式(您发送消息 ID 和消息到期),Apple 将返回错误响应,而您没有使用.在您的情况下,无效令牌只会关闭套接字,但您无法知道是哪条消息导致了问题.

In case of an invalid device token, Apple will return an error response if you use the newer binary format (in which you send a message id and message expiry), which you don't. In your case an invalid token will simply close the socket, but you'll have no way to know which message caused the problem.

您应该在此处阅读有关错误检查的信息.您应该阅读有关格式 此处.

You should read about error checking here. You should read about the format here.

这篇关于批量/多个 iOS 推送通知代码 - 适用于 2 台设备,但不适用于 100 台设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆