ios - 批量/多个 iOS 推送通知代码 - 适用于 2 台设备,但不适用于 100 台设备

标签 ios push-notification apple-push-notifications

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

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

我的客户真的被黑了。谁能帮忙??

函数 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.";

最佳答案

您的数据库中可能有一些无效的设备 token 。

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

您应该阅读错误检查 here .您应该阅读格式 here .

关于ios - 批量/多个 iOS 推送通知代码 - 适用于 2 台设备,但不适用于 100 台设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17116249/

相关文章:

php - 推送通知在android中多次获取

ios - 您可以在 Apple Push Notification 中塞入多少个字符?

android - 动画中的英雄和showDialog动画

ios - 应用程序提供的自定义字体不起作用

ios - 选择其他文本字段时如何重置 UIPickerView 内的值?

ios - 应用程序使用 swift 使用 API 时会崩溃吗?

android - 没有从 gcm 获取 android 推送通知消息

Android 和(数百万)推送通知

objective-c - Apple 推送通知反馈服务 - 检查频率

push-notification - 我们如何跟踪从 IBM MobileFirst 7.0 发送到 Apple APNS 服务器的推送通知?