我的应用程序允许用户在未来设置一些提醒。当应用程序启动时,我想知道已经设置了哪些提醒(通知)。
我可以读回我设置的通知还是我需要存储在我的应用程序中(例如 Core Data 或 Plist)?
我的应用程序允许用户在未来设置一些提醒。当应用程序启动时,我想知道已经设置了哪些提醒(通知)。
我可以读回我设置的通知还是我需要存储在我的应用程序中(例如 Core Data 或 Plist)?
UIApplication
有一个名为的属性scheduledLocalNotifications
,它返回一个包含类型元素的可选数组UILocalNotification
。
UIApplication.shared.scheduledLocalNotifications
对于 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)
}
}
}
斯科特是正确的。
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
@Scott Berrevoets 给出了正确答案。要实际列出它们,枚举数组中的对象很简单:
[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];
斯威夫特 3.0.2:
UIApplication.shared.scheduledLocalNotifications
在iOS 10
,使用新UserNotifications
框架:
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
print("Requests: \(notificationRequest)")
}
斯威夫特 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.
}
}
})
我用它来修复一个错误,即相同的每周通知已经设置并在应用程序打开时再次设置,因此它会不断重置计时器以显示,这意味着它从未出现过。
在 Swift 中,要查看控制台中打印的所有当前计划的本地通知:
print(UIApplication.sharedApplication().scheduledLocalNotifications)