apns 在设备离线时丢弃了旧的推送通知

apns discarded the old push notification while device offline

我正在处理推送通知,我想使用 php 向 IOS 发送聊天推送通知。但是,当我向 apns(Apple 推送通知服务器)发送 5 个推送通知时,apns 会丢弃旧的推送通知,并在设备上线时仅向设备发送最新的推送通知。

我在互联网上搜索解决方案,找到的一种解决方案是设置通知到期时间。所以我通过我的实际问题没有解决来实现这个解决方案。

有什么办法可以解决我的问题。建议任何有用的解决方案或参考站点。

IOS push notification with PHP

下面是我的示例代码

public function sendIOSNotification($tokens, $data, $envoirement = 'production') {
        try {

            $payload = json_encode($this->setIosNotificationDataParameters($data));
            $deviceTokens = str_replace(array(' ', '<', '>'), '', $tokens['ios']);
            // FUNCTION NOTIFICATIONS   
            $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', config('push-notification.appNameIOS.certificate_' . $envoirement));
            stream_context_set_option($ctx, 'ssl', 'passphrase', 'push');
            //send notification 
            $fp = stream_socket_client(
                    config('push-notification.appNameIOS.ios_push_notification_' . $envoirement), $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx
            );
            $res = [];
            foreach ($deviceTokens as $deviceToken) {
                $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) 
                . pack('n', strlen($payload)) . $payload
                .pack('N', time()).pack('N', time() + 86400);
                $res = json_encode($result);
            }
            fclose($fp);
            \Log::info("=== IOS Notification Send Successfully ===");
            return true;
        } catch (\Exception $ex) {
            $messages = $ex->getMessage() . '::' . $ex->getFile() . '( ' . $ex->getLine() . ' )';
            \Log::ifno("===Push Notificaion Exception===");
            \Log::ifno($messages);
            return true;
        }
    }

你不能按照文档做你想做的事。这是记录在案的行为:

Quality of Service, Store-and-Forward, and Coalesced Notifications Apple Push Notification service includes a Quality of Service (QoS) component that performs a store-and-forward function. If APNs attempts to deliver a notification and the destination device is offline, APNs stores the notification for a limited period of time and delivers it when the device becomes available again. This component stores only the most recent notification per device and per app. If a device is offline, sending a notification request targeting that device causes the previous request to be discarded. If a device remains offline for a long time, all its stored notifications in APNs are discarded. source: apple push notifications documentation

这意味着每个用户每个应用只有一个离线通知。

您应该以不同的方式构建您的应用程序。首先,对于聊天应用程序,您不能指望在设备重新联机时发送数千条推送通知。如果你想在你的应用程序中显示它们,你将必须为你的应用程序实现一些额外的机制来检索旧消息