Can I dismiss the local notification when next time the app launch?

霸气de小男生 提交于 2019-12-08 01:08:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!