【问题标题】:Dismiss an already delivered UILocalNotification?关闭已交付的 UILocalNotification?
【发布时间】:2012-05-26 00:10:05
【问题描述】:

可以这样做吗? UIApplication's scheduledLocalNotifications 似乎不会返回已经发送到用户通知中心的通知,所以我认为这可能是设计使然,但我找不到任何书面证据。

有人知道吗?

谢谢!

编辑:发现这个:

您可以通过调用取消特定的预定通知 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”中。

【问题讨论】:

  • 你能显示一些代码,你是如何使用的吗?
  • 当然,刚刚用一些代码更新了问题。

标签: ios uilocalnotification uiapplication


【解决方案1】:

从 iOS10 开始,如果您已过渡到使用 UNUserNotificationCenter,则现在对此提供原生支持。

Apple 文档状态:

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

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

func removeDeliveredNotifications(withIdentifiers: [String])

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

func removeAllDeliveredNotifications()

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      您可以通过将新创建的通知添加到您自己的通知 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];
          }
      }
      

      【讨论】:

      • 只要您的应用程序保持常驻,这将起作用。它一辞职,currentNotifications 就会干杯。我希望也有一种方法可以列出已发送的通知,@elsurudo。
      • 这不适用于 ios 9.3.5,通知保留在通知中心
      【解决方案4】:

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

      幸运的是,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);
      

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

      【讨论】:

        【解决方案5】:

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

         [[UIApplication sharedApplication] cancelAllLocalNotifications];
        

        【讨论】:

        • 哦,哎呀。在我的实际代码中,我有一个关于 [app cancelLocalNotification:localNotification] 的条件,所以 cancelAllLocalNotifications 不会起作用。我将更新我的代码 sn-p 以匹配此内容。
        【解决方案6】:

        试试这个链接:

        Apple doc

        Some tutorial

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

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

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

        【讨论】:

        • 是的,我已经阅读了这些内容。但我想做的是在以后(任意)时间删除特定通知,而不仅仅是在第一次收到通知时。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-23
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多