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

前端 未结 12 1671
情深已故
情深已故 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:08

    If somebody wants it in swift 3.0

    switch application.applicationState {
        case .active:
            //app is currently active, can update badges count here
            break
        case .inactive:
            //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
            break
        case .background:
            //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
            break
        default:
            break
        }
    

    for swift 4

    switch UIApplication.shared.applicationState {
    case .active:
        //app is currently active, can update badges count here
        break
    case .inactive:
        //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
        break
    case .background:
        //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
        break
    default:
        break
    }
    
    0 讨论(0)
  • 2020-11-29 16:09

    According to iOS / XCode: how to know that app has been launched with a click on notification or on springboard app icon? you have to check for the application state in didReceiveLocalNotification like this:

    if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive)
    {
        // user has tapped notification
    }
    else
    {
        // user opened app from app icon
    }
    

    Although it does not make totally sense to me, it seems to work.

    0 讨论(0)
  • 2020-11-29 16:09

    For iOS 10 and above put this in AppDelegate, to get to know notification is tapped(works even app is closed or open)

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {
    print("notification tapped here")
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-29 16:13

    You can configure your push notification's payload to call app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method when the app is in background. You can set some flag here so that when user launch your application next time, you can perform your operation.

    From apple’s documentation you should use this methods to download new content associated with push notification. Also for this to work, you have to enable Remote notification from Background modes and your push notification payload must contain content-available key with its value set to 1. From more info please see Using Push Notifications to Initiate a Download section from apple doc here.

    Another way is to have badge count in push notification payload. So next time your application launches you can check application badge count. If its grater than zero, perform your operation and zero/clear badge count from server also.

    Hope this helps you.

    0 讨论(0)
  • 2020-11-29 16:14

    In my case, background mode OFF did not make any difference. However when the app was suspended and the user tapped the notification, I could handle the case in this callback method:

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
    
    }
    
    0 讨论(0)
提交回复
热议问题