【问题标题】:Find list of Local Notification the app has already set查找应用程序已设置的本地通知列表
【发布时间】:2013-07-06 01:30:01
【问题描述】:

我的应用程序允许用户在未来设置一些提醒。当应用启动时,我想知道已经设置了哪些提醒(通知)。

我可以回读我设置的通知还是需要存储在我的应用程序中的通知(例如 Core Data 或 Plist)?

【问题讨论】:

  • 有没有办法找到即将到来的通知? (包括有重复间隔的那个)

标签: ios uilocalnotification


【解决方案1】:

UIApplication 有一个名为scheduledLocalNotifications 的属性,它返回一个包含UILocalNotification 类型元素的可选数组。

UIApplication.shared.scheduledLocalNotifications

【讨论】:

  • 嘿,斯科特 Berrevoests,请帮帮我。我第二次没有收到警报,但它已经在待处理的警报列表中。 stackoverflow.com/questions/44132879/…
  • @ScottBerrevoets 当应用程序处于后台并接收 UNUserNotification 时执行哪个方法?
  • 请注意 scheduledLocalNotifications 已弃用
  • @MattLeFleur 有新的方法吗?我很难找到一个。
  • @HenningHall,我会使用 Frizzo 的答案来实现这一点 - stackoverflow.com/a/39034576/810167
【解决方案2】:

斯科特是正确的。

UIApplication的属性scheduledLocalNotifications

代码如下:

NSMutableArray *notifications = [[NSMutableArray alloc] init];
[notifications addObject:notification];
app.scheduledLocalNotifications = notifications;
//Equivalent: [app setScheduledLocalNotifications:notifications];

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;
    }
}

NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;

for (UILocalNotification *localNotification in arrayOfLocalNotifications) {

    if ([localNotification.alertBody isEqualToString:savedTitle]) {
        NSLog(@"the notification this is canceld is %@", localNotification.alertBody);

        [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system

    }

}

欲了解更多信息,请查看:scheduledLocalNotifications example UIApplication ios

【讨论】:

  • 感谢这也有助于解决我下一个关于为每个通知存储 ID 的问题 - 现在全部完成...
  • 这是一个奇怪的示例代码。 OP 要求列出本地通知,您正在展示如何取消本地通知的示例
【解决方案3】:

@Scott Berrevoets 给出了正确答案。要实际列出它们,枚举数组中的对象很简单:

[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
    NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];

【讨论】:

    【解决方案4】:

    在 Swift 中,查看控制台中打印的所有当前计划的本地通知:

    print(UIApplication.sharedApplication().scheduledLocalNotifications)
    

    【讨论】:

      【解决方案5】:

      对于 Swift 3.0 和 Swift 4.0

      别忘了import UserNotifications

      这适用于 iOS10+watchOS3+ 因为类 UNUserNotificationCenter 不适用于旧版本 (link)

      let center = UNUserNotificationCenter.current()
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
      
          center.getPendingNotificationRequests { (notifications) in
              print("Count: \(notifications.count)")
              for item in notifications {
                print(item.content)
              }
          }
      }
      

      【讨论】:

      • 当应用处于后台并收到 UNUserNotification 时,哪个方法会被执行?
      【解决方案6】:

      斯威夫特 3.0.2:

      UIApplication.shared.scheduledLocalNotifications
      

      【讨论】:

        【解决方案7】:

        iOS 10 中,使用新的UserNotifications 框架:

        UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
        
                print("Requests: \(notificationRequest)")
        }
        

        【讨论】:

        • 当应用处于后台并收到 UNUserNotification 时,哪个方法会被执行?
        【解决方案8】:

        斯威夫特 4

        UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (requests) in
        
            for request in requests {
        
                if request.identifier == "IDENTIFIER YOU'RE CHECKING IF EXISTS" {
        
                    //Notification already exists. Do stuff.
        
                } else if request === requests.last {
        
                    //All requests have already been checked and notification with identifier wasn't found. Do stuff.
        
                }
            }
        })
        

        我用它来修复一个错误,即相同的每周通知已经设置并在应用打开时再次设置,因此它会不断重置计时器以显示,这意味着它从未出现。

        【讨论】:

        • 如果我一开始用这个来检查是否有任何通知,它根本不会循环。
        • 当应用处于后台并收到 UNUserNotification 时,哪个方法会被执行?
        猜你喜欢
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多