Can I programmatically clear my app's notifications from the iOS 5 Notification Center?
Ios5Ios5 Problem Overview
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?
Ios5 Solutions
Solution 1 - Ios5
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 - Ios5
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 - Ios5
Yes, you can cancel specific or all local notifications by calling
[[UIApplication sharedApplication] cancelLocalNotification:...];
or
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Solution 4 - Ios5
If you want to clear notifications in swift and iOS 10.0
import UserNotifications
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
center.removeAllDeliveredNotifications() // To remove all delivered notifications
}
Solution 5 - Ios5
For me it only worked with sending a local notification with only a badge like this:
if([UIApplication sharedApplication].applicationIconBadgeNumber == 0) {
UILocalNotification *singleLocalPush = [[UILocalNotification alloc] init];
singleLocalPush.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
singleLocalPush.hasAction = NO;
singleLocalPush.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:singleLocalPush];
[singleLocalPush release];
} else {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
And in the method
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
I can set the badge to 0 again.