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

前端 未结 12 1670
情深已故
情深已故 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 15:49

    Solved with Deployent target change, after lots of trial and error.

    I was experiencing the same issue with iOS Deployment Target 9.0 and 10.0, XCode Version 11.6 (11E708). - did not get any delegate method calls from tapping the notification when the app is in background.

    What solved this was changing the Target to iOS 11.0. I finally started getting the calls on notification tap from cold start and background scenarios in the UNUserNotificationCenter delegate didReceive: implementation.

    Additionally, the willReceive: now also gets the call in the foreground scenario (as expected).

    The payload I'm using is following. Note, that there's no content-available, or mutable-content set.

    {
      "aps": {
        "sound": "default",
        "badge": 1,
        "alert": {
          "body": "test",
          "title": "test"
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-29 15:50

    SWIFT 5.1

    UIApplication.State did not work for me, because once I read fingerprint (modal is shown) in my app, notification is also thrown in upper bar and user must click it.

    I've created

    public static var isNotificationFromApp: Bool = false

    in AppDelegate and I set it true in my starting viewController and then in my notification storyboard/viewControllerI just check that :)

    Hope it can come in handy

    0 讨论(0)
  • 2020-11-29 15:56

    OK I finally figured out.

    In the target settings ➝ Capabilities tab ➝ Background Modes, if you check "Remote Notifications", application:didReceiveRemoteNotification: will get triggered as soon as notification arrives (as long as the app is in the background), and in that case there is no way to tell whether the user will tap on the notification.

    If you uncheck that box, application:didReceiveRemoteNotification: will be triggered only when you tap on the notification.

    It's a little strange that checking this box will change how one of the app delegate methods behaves. It would be nicer if that box is checked, Apple uses two different delegate methods for notification receive and notification tap. I think most of the developers always want to know if a notification is tapped on or not.

    Hopefully this will be helpful for anyone else who run into this issue. Apple also didn't document it clearly here so it took me a while to figure out.

    0 讨论(0)
  • 2020-11-29 15:57

    There are two Funcs to handle received PushNotification inside PushNotificationManager class:

    class PushNotificationManager: NSObject, MessagingDelegate, UNUserNotificationCenterDelegate{
    
    }
    

    As I tested the first on trigger as soon as Notification arrived

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
        completionHandler(UNNotificationPresentationOptions.alert)
        
        //OnReceive Notification
        let userInfo = notification.request.content.userInfo
        for key in userInfo.keys {
             Constants.setPrint("\(key): \(userInfo[key])")
        }
        
        completionHandler([])
        
    }
    

    And second one when Tapped on Notification:

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
        //OnTap Notification
        let userInfo = response.notification.request.content.userInfo
        for key in userInfo.keys {
            Constants.setPrint("\(key): \(userInfo[key])")
        }
        
        completionHandler()
    }
    

    I also tested it with both ON and OFF states of Remote Notification(in Background Modes)

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

    If you have "Background Modes" > "Remote notifications" checked == YES, tap on notification event will arrive in:

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center **didReceiveNotificationResponse**:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler.
    

    It helped me. Please enjoy :)

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

    I ran into this problem, too — but on iOS 11 with the new UserNotifications Framework.

    Here for me it is like this:

    • New Launch: application:didFinishLaunchingWithOptions:
    • Received from a terminate state: application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
    • Received in Foreground: userNotificationCenter(_:willPresent:withCompletionHandler:)
    • Received in Background: userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
    0 讨论(0)
提交回复
热议问题