Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apns discarded the old push notification while device offline

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']);
            // 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;
        }
    }
like image 569
Rizwan Saleem Avatar asked Jan 10 '19 10:01

Rizwan Saleem


People also ask

Do push notifications work offline?

Good news: You can send web push notifications to your subscribers even if they're offline. They can still see your notifications when they come back online.

Do push notifications disappear?

Do push notifications disappear? With iOS notifications, the message disappears once the device is unlocked. But Android notifications remain in the notification center until an action is taken. Web push notifications in Chrome disappear when users leave that screen.

How do I fix missing push notifications entitlement?

If your app uses the Apple Push Notification service, make sure your App ID is enabled for Push Notification in the Provisioning Portal, and resubmit after signing your app with a Distribution provisioning profile that includes the 'aps-environment' entitlement.


1 Answers

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

like image 148
Moreno Avatar answered Oct 15 '22 10:10

Moreno