Adding delay to avoid notification alert getting lost in iOS

Sometimes, in iOS devices, if the notification opens when the application is not running or is in the background, then the application cannot read the notification payload, which results in an alert view not getting displayed.

It happens because the application takes more time to load all the libraries and in meantime JS tries to display the alert.

Normally, when the application runs in the foreground or background, the notification alert does not get lost as the application is fully loaded prior to notification.

To display the notification alert, add wait/delay in didFinishLaunchingWithOptions so that the alert view of the notification gets displayed.

use the following snippet to add inside the AppDelegate.m class under the didFinishLaunchingWithOptions method:

if (launchOptions != nil) {
     NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
     if(userInfo.allKeys.count > 0) {
       // You can add any dispatch delay
		dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
          [[NSNotificationCenter defaultCenter] postNotificationName:@"CDVMFPPushDidReceiveRemoteNotification" object:userInfo];     
            });
        }
   }
Last modified on March 12, 2021