Detect if application: didReceiveRemoteNotification: fetchCompletionHandler: was called by tapping on a notification in Notification Center

前端 未结 8 1805
北荒
北荒 2021-01-30 16:38
application: didReceiveRemoteNotification: fetchCompletionHandler:

is different from

application: didReceiveRemoteNotification:
         


        
8条回答
  •  离开以前
    2021-01-30 17:17

    The Apple docs are a bit confusing

    application: didReceiveRemoteNotification: fetchCompletionHandler:  
    

    is used if your application supports the remote-notification background mode (ie you're doing BackgroundFetch.)

    application: didReceiveRemoteNotification:  
    

    is called when the OS receives a RemoteNotification and the app is running (in the background/suspended or in the foreground.)
    You can check the UIApplicationState to see if the app was brought to foreground by the user (tapping on notification) or was already running when notification comes in.

    - (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
          UIApplicationState state = [application applicationState];
            // user tapped notification while app was in background
        if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
             // go to screen relevant to Notification content
        } else {
             // App is in UIApplicationStateActive (running in foreground)
             // perhaps show an UIAlertView
        }
    }
    

提交回复
热议问题