0

Hi I am trying to remove a notification from the notification center when my app is on the background and a silent push arrives. It works fine if the app is in Foreground or connected to debugger.

However when the apps is on background it doesn't work. I'm using in removeDeliveredNotificationsWithIdentifiers inside didReceiveRemoteNotification. Any advice ? Thank you

[center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {

   for (UNNotification* notification in notifications) 
   {
         NSDictionary* ui = notification.request.content.userInfo;

         if ([ui[@"activityId"] isEqualToString:activityId]) 
         {            
             [center removeDeliveredNotificationsWithIdentifiers:@[notification.request.identifier]];
         }

    }
    completionHandler(UIBackgroundFetchResultNoData);                     
}]; 
Kampai
  • 22,848
  • 21
  • 95
  • 95
archilo20
  • 41
  • 5
  • Please refer to this below link might help https://stackoverflow.com/questions/46689396/how-to-clear-a-remote-pushed-notification-for-ios – Atmaram Aug 22 '19 at 05:54

2 Answers2

0

Try Using UIApplicationState property.

Sample:

  UIApplicationState* applicationState;
  if (applicationState == UIApplicationStateActive) {
    // Application is running in foreground
    //Handler 
   }
   else if (applicationState == UIApplicationStateBackground || applicationState == UIApplicationStateInactive) {
   // Application is brought from background or launched after terminated
   // Handler 
   }
Bhavin
  • 1
  • Hi Bhavin, thanks for you answer. My issue is that the code is not executed when the app is in background, how would this help me ? Thank you – archilo20 Aug 20 '19 at 17:23
0

What is "doesn't work" meaning? The didReceiveRemoteNotification is not called, or removeDeliveredNotificationsWithIdentifiers not work?

There are two didReceiveRemoteNotification methods, one of them is deprecated.

application:didReceiveRemoteNotification:(deprecated) application:didReceiveRemoteNotification:fetchCompletionHandler:

And in Apple doc:

Unlike the application:didReceiveRemoteNotification: method, 
which is called only when your app is running in the foreground, 
the system calls this method when your app is running in the foreground or background. 

Or you can try: userNotificationCenter:willPresentNotification:withCompletionHandler:(Apple recommand)

Apple doc:

When a device receives a background notification, the system wakes your app in the background. On iOS it calls your app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. On watchOS, it calls your extension delegate’s didReceiveRemoteNotification:fetchCompletionHandler: method. Your app has 30 seconds to perform any tasks and call the provided completion handler. For more information, see Handling Notifications and Notification-Related Actions.

EDIT: @archilo20 Do your project enable background modes?

This setting is in the target->Capabilities->Background Modes->Remote notifications

Make sure you enable it.

EDIT 28/08:

Try this answer:

Answer

The key points are : 1.content-available:1 and mutable-content:1 in push notification payload. 2.Enable Background Fetch.

irons163
  • 54
  • 6
  • Hi irons thank you for your answer, my problem is that didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler is not being called when silent push arrives and the app is on the background. Everything works fine if the app is in the foreground. Thanks for the advice – archilo20 Aug 26 '19 at 16:13
  • Thank you for you reply irons. Yes background modes is enabled. – archilo20 Aug 27 '19 at 11:19