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

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

+47
ios5
Oct. 14 2018-11-21T00:
source share
5 answers

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

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 

This only works if the number changes, so if your application does not use the icon number that you must first set, then reset it.

 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
+71
Oct 19 '11 at 13:25
source share

The simpler method that I use (and does not require icons) relates to resetting an array of scheduled local notifications for myself as follows:

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

This means that any scheduled notifications remain valid, and all "old" notifications that are present in the Notification Center are deleted. However, it also has a sense of something that might change in a future version of iOS, as I have not seen any documentation for this behavior.

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

  [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
+19
Feb 14 2018-12-12T00:
source share

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

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

or

 [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
+3
Jan 18 2018-12-18T00:
source share

For me, it worked only with sending a local notification with only the same icon:

  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 0 icon again.

+1
Nov 04 '11 at 10:10
source share

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 } 
+1
Jan 11 '17 at 6:44
source share



All Articles