Calling didReceiveRemoteNotification when app is launching for the first time Calling didReceiveRemoteNotification when app is launching for the first time xcode xcode

Calling didReceiveRemoteNotification when app is launching for the first time


You should add something like this to your code :

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary    *)launchOptions {        NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];        //Accept push notification when app is not open        if (remoteNotif) {                  [self handleRemoteNotification:application userInfo:remoteNotif];            return YES;        }        return YES;    }

You can move the logic from didReceiveRemoteNotification to some common function and call that function from both places. This will only work if the user open the app by tapping the notification. If the user opens the app by tapping the app icon, the notification data won't reach the app.


Swift Code

let remoteNotif: AnyObject? = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey]            //Accept push notification when app is not open            if ((remoteNotif) != nil) {                self.handleRemoteNotification(remoteNotif!)            }func handleRemoteNotification(remoteNotif: AnyObject?){//handle your notification here}

@Eran thanks :)