Removing UILocalNotification from notification tray programmatically

Mr.Anonymous picture Mr.Anonymous · Jan 3, 2013 · Viewed 11k times · Source

Is there a way to programmatically remove/dismiss UILocalNotification from Notification Tray. I am able to cancel the notification which removes the notifications from

[[UIApplication sharedApplication] scheduledLocalNotifications]

Here is what i need to do

I need to dismiss the UILocalNotification from NotificationTray after the action has been performed(ie after the user taps the notification)

EDIT: I can remove the notifications from the NSNotificationCenter. I want to remove specific notifications from the Notification Tray .Like the user presses the clear button to clear all the notifications belonging to a particular application.

Answer

Midhun MP picture Midhun MP · Jan 3, 2013

You can cancel all notifications using:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

If you want to remove a particular notification, you can use userinfo of notification object, when you create a local notification add a unique ID to that. Later you can use that ID for removing local notification.

For that you can use the following code:

NSString *notificationId = @"id_to_cancel";
UILocalNotification *notification = nil;
for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
  if([[notify.userInfo objectForKey:@"ID"] isEqualToString:notificationId])
  {
     notification = notify;
     break;
  }
}
[[UIApplication sharedApplication] cancelLocalNotification:notification];