How to find out when an application received a notification and when a user clicked on a notification in iOS

I know that a lot has been written about this topic, but I just cannot find the right answer.

Is there any way to know when the user received the remote notification and when the user clicked one on iOS 8. I would like to know this because when I receive it I want to save it and when the user clicks on it I want to open some that view.

I found this answer https://stackoverflow.com/a/166268/2126/ , but the problem is that the user is in the application and opens the notification center and clicks on it, the application is not inactive and not in the background.

I also found this answer at https://stackoverflow.com/a/464616/2/2 , but I know that this only runs when the application is killed and launched from a new one.

I also don’t want to do this https://stackoverflow.com/a/2129441/232632 because I need to determine when I received the notification.

So, is there a way to find out if the user clicked only the notification. As I understand it, this needs to be done in didReceiveRemoteNotification, but I do not know how to separate them. And I need an answer for iOS 10, because the purpose of the application is iOS 8.

MY DECISION:

, , , , , . , , .

+4
2

UNUserNotificationCenterDelegate userNotificationCenter(_:willPresent:withCompletionHandler:) userNotificationCenter(_:didReceive:withCompletionHandler:), , . willPresent: completionHandler , , , , .

:

UNUserNotificationCenter.current().delegate = self

, :

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler(UNNotificationPresentationOptions.alert)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let userInfo = userInfo as? [String: Any] {
        // TODO: implement your logic
        // just don't forget to dispatch UI stuff on main thread
    }
}

AppDelegate, NSObject, , AppDelegate .

P.S.: , , (UNUserNotificationCenter.current().requestAuthorization(options:completionHandler:)), (UIApplication.shared.registerForRemoteNotifications()).

, - , ( ).

+2

ios 10 , ,

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

, , AppDelegate :

class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  var isUserTapOnNotification = false

true isUserTapOnNotification

func applicationWillEnterForeground(_ application: UIApplication) {
        isUserTapOnNotification = tue
    }

, , applicationWillEnterForeground , didReceiveRemoteNotification :

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

  if #available(iOS 10.0, *) {
//do nothing

}else { //<ios 10
 if isUserTapOnNotification == true {//when app is in background and user tap on notification bar
  //do action whatever you want
} else { //when user is in foreground and notification came,
  //before ios10,notification bar not display in foreground mode,So you can show popup by using userInfo
}

}

applicationDidBecomeActive , reset isUserTapOnNotification - false :

func applicationDidBecomeActive(_ application: UIApplication) {
       isUserTapOnNotification = false
    }

, .

+2

Source: https://habr.com/ru/post/1694340/


All Articles