【问题标题】:iOS application push notificationsiOS 应用程序推送通知
【发布时间】:2016-08-16 13:43:39
【问题描述】:

我们有一个内置 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);

有人对此有任何想法吗?

非常感谢

【问题讨论】:

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


    【解决方案1】:

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

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

    还有:

    错误响应。使用旧格式,如果您发送通知 以某种方式格式错误的数据包 - 例如,有效负载超过 规定的限制——APNs 通过切断连接做出响应。它 没有说明它拒绝通知的原因。增强型 格式允许提供者使用任意标记通知 标识符。如果有错误,APNs 会返回一个数据包 将错误代码与标识符相关联。此响应使 提供者来定位和纠正格式错误的通知。

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

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

    【讨论】:

      【解决方案2】:

      一旦发生任何错误(无效的设备令牌、格式错误的负载等),AFAIK APNs 将立即关闭连接。您发送 Push 的代码需要准备好从这种情况中恢复,重新连接,然后重新开始工作。

      看看那里的 3rdParty 库来为您执行这项工作。您将依赖社区工作。

      我可以推荐Pushy,它是用 Java 编写的。我在 2 个不同的项目中使用过它,我可以说它运行良好,而且它是一个非常活跃的项目(那里有很多讨论和更新)。

      我不知道他们中的任何一个使用 PHP,但可能有一个

      【讨论】:

        猜你喜欢
        • 2012-10-24
        • 1970-01-01
        • 1970-01-01
        • 2018-08-15
        • 1970-01-01
        • 2016-08-22
        • 1970-01-01
        • 1970-01-01
        • 2017-01-24
        相关资源
        最近更新 更多