php - iOS 应用程序推送通知

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

我们有一个用 PHP 构建的应用程序,可以向 Android 和 iOS 发送推送通知。我们的问题是 iOS 的某些设备 ID 似乎完全停止了我们脚本中所有其他 iOS 推送通知的发送,他们甚至说它们已被无误发送,但它随后停止了循环中的所有通知。

如果我们随后从数据库中删除有问题的设备 ID,则发送脚本适用于有问题的设备之后的所有设备。这很奇怪,似乎无法弄清楚为什么。

有没有人有这方面的经验?发送到不再存在的设备 ID 是否会阻止苹果在该特定连接上完成我们的脚本?

这是我们用 PHP 编写的发送脚本(这适用于除了奇怪的恶意设备 ID 之外的所有设备):

    $tHost = 'gateway.push.apple.com';
    $tPort = 2195;
    $tCert = "path_to_our_cert.pem";
    $tPassphrase = 'passphrase';

    $tAlert = $this->title; //assign a title
    $tBadge = 8;
    $tSound = 'default';
    $tPayload = $this->body_plain; //assign a body

            // Create the message content that is to be sent to the device.
    $tBody['aps'] = array ('alert' => $tAlert,'badge' => $tBadge,'sound' => $tSound,);
    $tBody ['payload'] = $tPayload;
    $tBody = json_encode ($tBody);

    $tContext = stream_context_create ();
    stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert);
    stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase);
    $tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);   

    if (!$tSocket) exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);
    //Loop through all devices to send
    foreach($this->device->devices as $item){
        if($item->os != "iOS") continue;                
        if(session::getAdministratorStaffSchoolID() != $item->school_id) continue;
        $tToken = $item->device_id;//assign the device id
        $tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;
            $tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));
    }

    fclose ($tSocket);

有没有人对此有任何想法?

非常感谢

最佳答案

所以,只是一个想法,但您正在使用旧格式发送通知:

$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;

和:

Error response. With the legacy format, if you send a notification packet that is malformed in some way—for example, the payload exceeds the stipulated limit—APNs responds by severing the connection. It gives no indication why it rejected the notification. The enhanced format lets a provider tag a notification with an arbitrary identifier. If there is an error, APNs returns a packet that associates an error code with the identifier. This response enables the provider to locate and correct the malformed notification.

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Appendixes/LegacyFormat.html

所以也许 APNS 只是断开了对您的连接?这就是为什么所有剩余的通知实际上都没有通过的原因。仔细看看这些有效载荷。可能是转向新格式的好时机。

关于php - iOS 应用程序推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38976604/

相关文章:

javascript - 使用ajax上传图片和表单提交

iphone - UINavigationController 对 View 进行动画处理,但不对标题进行动画处理

ios - Parse.com 查询超过 1000 个对象

iphone - 标签栏 Controller 消失

iphone - NS 字符串比较因 stringWithFormat 失败

php - 在 PHP 脚本中保留 MySQL 注释

php - 字符串转十六进制颜色代码

php - 使用 PHP 检测历史变化?

ios - 你如何从 Objective C 中的一个 block 返回

ios - 用户取消 IAP ios sdk 时如何更新 UI