问题
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.
回答1:
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)
回答2:
If you save a link to your notification, then you will can cancel it before it fires.
[[UIApplication sharedApplication]cancelLocalNotification:yourNotification];
回答3:
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];
来源:https://stackoverflow.com/questions/14618345/can-i-dismiss-the-local-notification-when-next-time-the-app-launch