Abhinav
Abhinav

Reputation: 38162

Programmatically delete Remote Notifications from Notification Centre

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.

Upvotes: 4

Views: 4171

Answers (5)

Junaid Rehmat
Junaid Rehmat

Reputation: 335

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

Upvotes: 0

Bilal
Bilal

Reputation: 19156

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

Upvotes: 0

Abhinav
Abhinav

Reputation: 38162

Finally...

This one works like charm!

[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];

Upvotes: 12

Mehul Parmar
Mehul Parmar

Reputation: 3699

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.

Upvotes: 0

Himanth
Himanth

Reputation: 2446

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

Upvotes: 0

Related Questions