查找应用程序已经设置的本地通知列表

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

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

UIApplication有一个名为scheduledLocalNotifications的属性,您可以使用它。

斯科特是正确的。

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示例UIApplication ios

对于Swift 3.0

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

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

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

Swift 3.0.2:

 UIApplication.shared.scheduledLocalNotifications 

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

 print(UIApplication.sharedApplication().scheduledLocalNotifications) 

iOS 10 ,使用新的UserNotifications框架:

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