willPresentNotification is called when app is in foreground. Have a look to their documentation
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
// The method will be called on the delegate only if the application is in the foreground.
// If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented.
// The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list.
// This decision should be based on whether the information in the notification is otherwise visible to the user.
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler {
// The method will be called on the delegate when the user responded to the notification by opening the application,
// dismissing the notification or choosing a UNNotificationAction.
// The delegate must be set before the application returns from applicationDidFinishLaunching:.
}
Try to check in didReceiveNotificationResponse
you will get what you need.
ALSO If need to fetch any data or any processing, Enable background fetch in background modes and use below method
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
completionHandler(UIBackgroundFetchResultNewData);
}
Handling APNS on the basis of application states
if(application.applicationState == UIApplicationStateInactive)
{
/*
# App is transitioning from background to foreground (user taps notification), do what you need when user taps here!
*/
}
else if(application.applicationState == UIApplicationStateActive)
{
/*
# App is currently active, can update badges count here
*/
}
else if(application.applicationState == UIApplicationStateBackground)
{
/* # App is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here */
}