Calling didReceiveRemoteNotification when app is launching for the first time

后端 未结 2 1874
后悔当初
后悔当初 2021-01-31 18:27

I have implemented my didReceiveRemoteNotification method. It works and displays a view controller with the notification data which is passed through. This only works when the a

2条回答
  •  心在旅途
    2021-01-31 19:01

    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.

提交回复
热议问题