Apns Discarded The Old Push Notification While Device Offline

- 1 answer

I am working on push notifications and I want to send chat push notifications to IOS using php. But when I send 5 push notifications to apns (Apple Push Notification Server) then apns discard the old push notification and send only the most recent push notification to device when device get online.

I search the for solution on internet and one solution that I found is to set the notification expiry time. So I implemented this solution by my actual problem not solved.

Is there any way that solve my problem. Suggest any usefull solution or reference site.

IOS push notification with PHP

Below is my sample code

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

            $payload = json_encode($this->setIosNotificationDataParameters($data));
            $deviceTokens = str_replace(array(' ', '<', '>'), '', $tokens['ios']);
            $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);
            \Log::info("=== IOS Notification Send Successfully ===");
            return true;
        } catch (\Exception $ex) {
            $messages = $ex->getMessage() . '::' . $ex->getFile() . '( ' . $ex->getLine() . ' )';
            \Log::ifno("===Push Notificaion Exception===");
            return true;


You cannot do what you intend according to the documentation. It is the documented behavior:

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

This means just an offline notification per user per app.

You should architecture your application in a different way. First of all, for a chat application you cannot expect sending thousand push notifications when the device is back online. You will have to implement some extra mechanism for your app to retrieve the old messages if you want to show them in your app