简体   繁体   English

从NotificationCenter删除LocalNotifications(iOS 6)

[英]Deleting LocalNotifications from NotificationCenter (iOS 6)

I have a problem with one aspect of managing LocalNotifications - deleting single notification in iOS 6. 我在管理LocalNotifications的一个方面遇到问题 - 在iOS 6中删除单个通知。

在此输入图像描述

I am able to create a LocalNotification with text content and fire date and it works. 我能够创建一个带有文本内容和开火日期的LocalNotification并且它可以工作。

On iOS 5 notification occurs as an AlertView on home screen with two buttons, and dissapears after touching either. 在iOS 5上,通知在主屏幕上显示为带有两个按钮的AlertView,并在触摸后消失。 I can cancel the fire of notification using [[UIApplication sharedApplication] cancelLocalNotification:theNotification] . 我可以使用[[UIApplication sharedApplication] cancelLocalNotification:theNotification]取消通知之火。 There is no problem here. 这里没有问题。

In iOS 6 notification occurs in a Notificaton Center as shown above when fired. 在iOS 6中,通知发生在Notificaton Center中,如上所示。 If I cancel it using [[UIApplication sharedApplication] cancelLocalNotification:theNotification] , it won't fire - it works. 如果我使用[[UIApplication sharedApplication] cancelLocalNotification:theNotification]取消它,它将不会触发 - 它可以工作。 But after it fires... 但在它发射之后......

My problem: 我的问题:

I am not able to erase this single fired notification from the Notification Center. 我无法从通知中心删除此单个已解雇的通知。 For example, I would like the notification to dissappear after it is touched, or after some action inside application is done. 例如,我希望通知在触摸后消失,或者在应用程序内部执行某些操作后消失。

What I tried: 我尝试了什么:

  • using [[UIApplication sharedApplication] cancelLocalNotification:theNotification] - cancels notification fire (actually there's no need since the notification did fire already), but doesn't erase fired notification from Notification Center 使用[[UIApplication sharedApplication] cancelLocalNotification:theNotification] - 取消通知触发(实际上没有必要,因为通知已经触发),但不会删除通知中心发出的通知
  • removing items from [[UIApplication sharedApplication] scheduledLocalNotifications] array - doesn't work, because this function always returns empty array, no matter how many (working!) notifications are set (does anybody know why?) [[UIApplication sharedApplication] scheduledLocalNotifications]数组中删除项目 - 不起作用,因为无论设置了多少(工作!)通知,此函数总是返回空数组(有人知道为什么吗?)

What I'm doing now 我现在在做什么

I am deleting all notifications using [[UIApplication sharedApplication] cancelAllLocalNotifications] - it cancels them AND erases from Notification Center, and then create them back, without the one I wanted to erase. 我正在使用[[UIApplication sharedApplication] cancelAllLocalNotifications]删除所有通知 - 它取消它们并从通知中心删除,然后创建它们,没有我想要删除的那个。

As you can see, this is rather idiotic and redundant solution, but I couldn't find better so far. 正如你所看到的,这是相当愚蠢和冗余的解决方案,但到目前为止我找不到更好的结果。

Any suggestions? 有什么建议?

Try This.....May be this help...Write this code from where you want to clear all your notifications. 试试这个.....可能是这个帮助...从你要清除所有通知的地方写下这段代码。

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

To erase one single notification 删除一个通知

You can get an array of scheduled notification from: @property(nonatomic,copy) NSArray *scheduledLocalNotifications 您可以从以下位置获取一系列预定通知: @property(nonatomic,copy) NSArray *scheduledLocalNotifications

Get the one you want by the index number of your choosing and then pass the UILocalNotification* to - (void)cancelLocalNotification:(UILocalNotification *)notification. 通过您选择的索引号获取您想要的那个,然后将UILocalNotification *传递给- (void)cancelLocalNotification:(UILocalNotification *)notification.

You can save a unique value for key in your local notification's userinfo. 您可以在本地通知的userinfo中为密钥保存唯一值。 Get all local notification, loop through the array and delete the particular notification. 获取所有本地通知,遍历数组并删除特定通知。

Code as follows, 代码如下,

UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    NSDictionary *userInfoCurrent = oneEvent.userInfo;
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
    if ([uid isEqualToString:uidtodelete])
    {
        //Cancelling local notification
        [app cancelLocalNotification:oneEvent];
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM