1

I have to fire two different experiments when app is in background state,

  1. When notification arrives to user but not tap yet. (this is not achieve)
  2. 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)
kulss
  • 2,057
  • 1
  • 14
  • 16
  • Try silent push for such requirement. https://stackoverflow.com/questions/36694963/what-is-silent-push-notification-when-does-the-device-receive-it – Y_Y Jul 02 '18 at 12:10
  • silent push is not in requirement because business wants user action on each push, and want to count among all the notifications which one user choose and tap. – kulss Jul 03 '18 at 04:52
  • After getting silent push you can generate local notification on which user taps and you will get the tap count. – Y_Y Jul 03 '18 at 06:30

1 Answers1

0

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:

Implement this method if your app supports the remote-notification background mode. ... 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. Use this method to download any data related to the push notification. When your method is done, call the block in the handler parameter.

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app.

About the use of above method:

It tells the app that a remote notification arrived that indicates there is data to be fetched.

Arnab
  • 4,216
  • 2
  • 28
  • 50
  • This method is also trigger only when user tap on the notification message, but my requirement is to get trigger when notification first comes in my phone before user tap on the alert messge – kulss Jul 02 '18 at 11:14