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

时间:2013-07-08 16:15:31

标签: ios uilocalnotification

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

我可以回读我设置的通知,还是需要存储在我的应用中(例如核心数据或Plist)?

8 个答案:

答案 0 :(得分:42)

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

答案 1 :(得分:21)

斯科特是对的。

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

答案 2 :(得分:21)

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

答案 3 :(得分:6)

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

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

答案 4 :(得分:4)

Swift 3.0.2:

UIApplication.shared.scheduledLocalNotifications

答案 5 :(得分:2)

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

UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in

        print("Requests: \(notificationRequest)")
}

答案 6 :(得分:1)

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

print(UIApplication.sharedApplication().scheduledLocalNotifications)

答案 7 :(得分:1)

Swift 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.

        }
    }
})

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