I have to fire two different experiments when app is in background state,
- When notification arrives to user but not tap yet. (this is not achieve)
- Out of all notifications, how many time user tap on notification alerts and open app. (this is achieved)
I used below methods : When the user responds to a notification by tap on alert message, the system calls below method with the results.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
If your app is in the foreground and a notification arrives, the notification center calls below method to deliver the notification directly to your app.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
You can use
application:didReceiveRemoteNotification:fetchCompletionHandler:method for that. When a push notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. From Apple's Docs:About the use of above method: