2

I know I can cancel the notification when user tap this notification in notification center . But can I cancel the notification in other palce where I can't get the related local notification from system. Can I serialize the local notification, and cancel it when the app runs next time?

Sorry for make you misunderstand!

I want to dismiss a posted notification in the notification center, but not a scheduled one. So what I want to ask is how to save the local notification object, then I can use it dismiss itself when next time the app launch. Maybe this job can't be done with current sdk.

NewXcoder
  • 705
  • 1
  • 5
  • 18

3 Answers3

2

If you need to cancel all notification you can use:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

For cancelling a particular notification:

[[UIApplication sharedApplication] cancelLocalNotification:aNotification];

For getting the particular Notification you can use:

NSArray *notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i = 0; i < [notifArray count]; i++)
{
    UILocalNotification *aEvent = [notifArray objectAtIndex:i];
    NSDictionary *userInfo = aEvent.userInfo;
    NSString *notifId=[NSString stringWithFormat:@"%@",[userInfo valueForKey:@"id"]];
    if ([id isEqualToString:cancelId])
    {
        [[UIApplication sharedApplication] cancelLocalNotification:aEvent];
        break;
    }
}

Here:

  • You need to store a id key value pair in the userInfo of your notification for identifying particular local notification
  • cancelId is the id of notification which you want to cancel (Stored in user info)
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • I think this is not I want. I want to dismiss a posted notification in the notification center, but not a scheduled one – NewXcoder Jan 31 '13 at 07:04
  • This does not answer the question. The question deals with cancelling already delivered notifications, not scheduled ones. – steveluscher May 14 '13 at 00:47
  • @steveluscher: You are correct, but he edited his question after I answered, Please check the edited date. The bold part was not there in his original question. – Midhun MP May 14 '13 at 04:24
1

Use this Code to get all scheduled notifications:

NSArray *reminderArray=[[UIApplication sharedApplication]scheduledLocalNotifications];

Then you can select the notification required and delete it.

[[UIApplication sharedApplication]cancelLocalNotification:yourNotification];
Vinayak Kini
  • 2,919
  • 21
  • 35
  • This does not answer the question. The question deals with cancelling already delivered notifications, not scheduled ones. – steveluscher May 14 '13 at 00:46
1

If you save a link to your notification, then you will can cancel it before it fires.

[[UIApplication sharedApplication]cancelLocalNotification:yourNotification];