Calling didReceiveRemoteNotification when app is launching for the first time

后端 未结 2 1873
后悔当初
后悔当初 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.

    0 讨论(0)
  • 2021-01-31 19:06

    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 :)

    0 讨论(0)
提交回复
热议问题