解雇已经发送的UILocalNotification?

时间:2012-05-18 12:00:25

标签: ios uilocalnotification uiapplication

有可能这样做吗? UIApplication's scheduledLocalNotifications似乎没有返回已经发送到用户通知中心的通知,因此我认为这可能是设计上的,但我找不到任何有关此问题的书面证据。< / p>

有人知道吗?

谢谢!

编辑:发现这个:

  

您可以通过致电取消特定的预定通知   cancelLocalNotification:在应用程序对象上,您可以取消   通过调用cancelAllLocalNotifications来调度所有预定通知。   这两种方法也以编程方式解除当前的

此处:http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html

但是,如果scheduledLocalNotifications未向我发送已发送的通知,如何获取已发送通知的引用?

编辑2:

在我注册了一些通知之后,这就是我要做的事情:

UIApplication *app = [UIApplication sharedApplication];

for (UILocalNotification *localNotification in app.scheduledLocalNotifications) 
{
     if (someCondition) {
            [app cancelLocalNotification:localNotification];
        }
     }
}

问题在于,一旦交付,他们就不再处于'scheduledLocalNotifications'。

6 个答案:

答案 0 :(得分:9)

您可以通过将新创建的通知添加到自己的NSMutableArray通知中来解决此问题,并检查该数组而不是app.scheduledLocalNotifications。 像这样:

NSMutableArray添加到Viewcontrollers .h文件中:

NSMutableArray *currentNotifications;

启动ViewController时启动它

currentNotifications = [[NSMutableArray alloc] init];

启动通知时,也请将其添加到您的阵列中:

UILocalNotification *notification = [[UILocalNotification alloc] init];
...
[currentNotifications addObject:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];

稍后,如果要取消该通知,请在数组中查找。 同时将其从阵列中删除:

for (UILocalNotification *notification in currentNotifications) {
    if (someCondition) {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
        [currentNotifications removeObject:notification];
    }
}

答案 1 :(得分:4)

自iOS10以来,如果您已转换为使用UNUserNotificationCenter,则现在支持此功能。

Apple文档声明:

func getDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> Void)

  

为您提供仍显示在通知中心的应用通知列表。

func removeDeliveredNotifications(withIdentifiers: [String])

  

从通知中心删除指定的通知。

func removeAllDeliveredNotifications()

  

从通知中心删除所有应用的通知。

答案 2 :(得分:2)

我一直在寻找答案。我的问题是我想要“清理”所有与通知中心相关的应用相关通知,以防万一用户从其停靠栏图标打开应用程序(因为这样做没有以前发出的通知...)

幸运的是,cancelAllNotifications似乎并不只是取消预定的,而是一切。所以我在爆破之前只是继续引用现有的预定通知,然后相应地重新安排它们:

UIApplication *app = [UIApplication sharedApplication];

NSLog(@"\nScheduled notif count (prior) = %d", app.scheduledLocalNotifications.count);

NSArray *scheduledNotifs = app.scheduledLocalNotifications; // hold on to a reference

[[UIApplication sharedApplication] cancelAllLocalNotifications]; // blast everything

NSLog(@"\nScheduled notif count (post-wipeout) = %d", app.scheduledLocalNotifications.count);

for (UILocalNotification *notif in scheduledNotifs) {
    [app scheduleLocalNotification:notif]; // put them back
}

NSLog(@"\nScheduled notif count (post-repopulation) = %d", app.scheduledLocalNotifications.count);

不确定这是否对任何人都有帮助,但这对我的情况非常有用。

答案 3 :(得分:1)

试试这个链接:

Apple doc

Some tutorial

本地通知正在注册到设备通知中心,而不是在您的应用中。

但是,如果您的应用正在运行,并且是通知时间,那么您可以在游戏中获取通知参数:

-(void) application:(UIApplication*)app didReceiveLocalNotification:(UILocalNotification*) notification
{
// local notification is geted
}

答案 4 :(得分:1)

如果您还将其保存在NSUserDefaults中,则可以在安排通知时执行此操作,使用NSKeyedArchiver将其转换为NSData。

要将其作为UILocalNotification取回,请使用NSKeyedUnarchiver。然后你可以使用cancelLocalNotification方法删除它。

完全解释here(Swift版本+原始Obj-C解决方案的链接)

答案 5 :(得分:0)

看到更新的代码后,您似乎有兴趣取消所有通知with-in for循环,因此您可以使用 -

 [[UIApplication sharedApplication] cancelAllLocalNotifications];