40

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

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

4

8 回答 8

43

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

UIApplication.shared.scheduledLocalNotifications
于 2013-07-08T16:18:27.070 回答
34

对于 Swift 3.0 和 Swift 4.0

不要忘记做import UserNotifications

这适用于iOS10+watchOS3+ ,因为UNUserNotificationCenter类不适用于旧版本(链接

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)
        }
    }
}
于 2016-08-19T08:35:41.210 回答
22

斯科特是正确的。

UIApplication的属性scheduleLocalNotifications

这是代码:

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

于 2013-07-08T16:32:41.437 回答
6

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

[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
    NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];
于 2014-06-29T02:03:30.327 回答
4

斯威夫特 3.0.2:

UIApplication.shared.scheduledLocalNotifications
于 2017-03-07T23:19:04.183 回答
3

iOS 10,使用新UserNotifications框架:

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

        print("Requests: \(notificationRequest)")
}
于 2017-06-01T23:59:55.877 回答
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.

        }
    }
})

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

于 2018-01-01T22:28:47.020 回答
1

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

print(UIApplication.sharedApplication().scheduledLocalNotifications)
于 2016-02-27T19:45:44.333 回答