iOS push notification: how to detect if the user tapped on notification when the app is in background?

前端 未结 12 1688
情深已故
情深已故 2020-11-29 15:43

There are a lot of stackoverflow threads regarding this topic, but I still didn\'t find a good solution.

If the app is not in the background, I can check launc

12条回答
  •  醉酒成梦
    2020-11-29 16:13

    I've been looking for the same thing as you and actually found a solution that does not require remote notification to be ticked off.

    To check whether user has tapped, or app is in background or is active, you just have to check the application state in

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    
        if(application.applicationState == UIApplicationStateActive) {
    
            //app is currently active, can update badges count here
    
        }else if(application.applicationState == UIApplicationStateBackground){
    
            //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
    
        }else if(application.applicationState == UIApplicationStateInactive){
    
            //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
    
        }
    

    For more info check:

    UIKit Framework Reference > UIApplication Class Reference > UIApplicationState

提交回复
热议问题