4

How can we pragmatically remove any pending remote notifications sent for my app from notification centre. I want to clear them up on app launch.

I have tried with [[UIApplication sharedApplication] cancelAllLocalNotifications]; API but its not helping.

PS: This question is specific to iOS 10 and old threads are not duplicates for this one.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • Possible duplicate of [Remove single remote notification from Notification Center](http://stackoverflow.com/questions/9925854/remove-single-remote-notification-from-notification-center) – Sargis May 12 '17 at 11:01
  • 1
    @SargisGevorgyan This cannot be marked as duplicate as the shared link talks about non-iOS 10 solution. iOS 10 handles it in a different way! – Abhinav May 12 '17 at 11:07

5 Answers5

12

Finally...

This one works like charm!

[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
Abhinav
  • 37,684
  • 43
  • 191
  • 309
0

You can clear all notifications from notification centre by using these simple lines of code

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];

use wherever you want. From my side I have used when user pressed logout. You can use in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

method to clear notifications after app opens

Himanth
  • 2,381
  • 3
  • 28
  • 41
0

AFAIK, you should do it by using the background mode for remote notifications, and then responding to these notifications by issuing a local notifications. You can remove local notifications, but not remote notifications.

Mehul Parmar
  • 3,599
  • 3
  • 26
  • 42
  • Thank you for your response. Could you please point me to documentation which confirms that remote notifications cannot be removed from Notification Centre? I am finding multiple versions online. – Abhinav May 12 '17 at 10:54
  • Can't find the official doc on this yet, but you can have a look here for now: http://stackoverflow.com/questions/24231120/remove-remote-notification-from-notification-center – Mehul Parmar May 12 '17 at 10:59
  • Its possible. Check out my answer. – Abhinav May 12 '17 at 11:02
0

Reseting the application badge number also remove all the notifications (local & remote) from the notification center.

Objective C

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

Swift

UIApplication.shared.applicationIconBadgeNumber = 0
Bilal
  • 18,478
  • 8
  • 57
  • 72
0

This can definitely be achieved by using removeDeliveredNotifications(withIdentifiers:) method available in UserNotifications.framework.

For a detailed tutorial, please follow this

https://medium.com/@sebastianosiski/implementing-removable-remote-notifications-on-ios-a17d74832bde

Junaid Rehmat
  • 305
  • 2
  • 15