Can I programmatically clear my app's notifications from the iOS 5 Notification Center?

I would like to remove old notifications that my app has made from the iOS 5 Notification Center. Can I do this? If so, how?


Solution 1:

To remove notifications from the Notification Center simply set your icon badge number to zero.

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

This only works if the number changes, so if your app doesn't use the badge number you have to first set, then reset it.

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

Solution 2:

A more straightforward method that I use (and doesn't require badges) is to reset the array of scheduled local notifications to itself, as follows:

  UIApplication* application = [UIApplication sharedApplication];
  NSArray* scheduledNotifications = [NSArray arrayWithArray:application.scheduledLocalNotifications];
  application.scheduledLocalNotifications = scheduledNotifications;

This has the effect that any scheduled notifications remain valid, while all "old" notifications that are present in Notification Center are removed. However, it also has the feel of something that might change in a future release of iOS, as I haven't seen any documentation for this behavior.

Of course, if you want to remove all notifications, it's simply the following:

  [[UIApplication sharedApplication] cancelAllLocalNotifications];

Solution 3:

Yes, you can cancel specific or all local notifications by calling

[[UIApplication sharedApplication] cancelLocalNotification:...]; 

or

[[UIApplication sharedApplication] cancelAllLocalNotifications];