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

问题描述 投票:30回答:8

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

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

ios uilocalnotification
8个回答
42
投票

UIApplication有一个名为scheduledLocalNotifications的房产,你可以使用。


22
投票

适用于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)
        }
    }
}

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


6
投票

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

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

4
投票

Swift 3.0.2:

UIApplication.shared.scheduledLocalNotifications

2
投票

iOS 10,使用新的UserNotifications框架:

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

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

2
投票

斯威夫特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.

        }
    }
})

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


1
投票

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

print(UIApplication.sharedApplication().scheduledLocalNotifications)
© www.soinside.com 2019 - 2024. All rights reserved.