How get notify when push notification arrive only, before tap

1k Views Asked by At

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)
1

There are 1 best solutions below

1
Arnab On

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.