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

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

提交回复
热议问题