- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler will not get Called when App run in foreground

Hi All,


I have an legacy app which was created for a couple of years ago, Recently We have to add to support ServiceExtension per apple request.


Before I implement the service extenion on my legacy project, I have created sample project by Xcode 11.2.1 and works fine.


on sample project, when App runs in foreground, If I send regular APNS with payload with mutable-content=1:


{"aps":{"alert":{"body":"myBody","title": "myTitle"},"mutable-content":"1"}}


it will invoke the method:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

{


once put those implementention on my legacy project, it never get call when receiving the same payload as sample project, it will only get called with content-available: 1


Where should I check on the configuration? or something else?


Thanks in advance.

Answered by daniel.ys in 415379022

Just dealt with this yesterday...


Implement UNUserNotificationCenterDelegate of UNUserNotificationCenter.currentNotificationCenter.

And right on app launch, set that delegate, for example:

[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];

The method:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

Will indicate how the notification will be presented once received, for example:

completionHandler(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge);


Then the method:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler

Will indicate what to do after the user interacted with said notification. Don forget to call the completion handler!
Good luck!

Accepted Answer

Just dealt with this yesterday...


Implement UNUserNotificationCenterDelegate of UNUserNotificationCenter.currentNotificationCenter.

And right on app launch, set that delegate, for example:

[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];

The method:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

Will indicate how the notification will be presented once received, for example:

completionHandler(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge);


Then the method:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler

Will indicate what to do after the user interacted with said notification. Don forget to call the completion handler!
Good luck!

Thanks.


That is great answer.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler will not get Called when App run in foreground
 
 
Q