ios 查找应用程序已设置的本地通知列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17531332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find list of Local Notification the app has already set
提问by Recycled Steel
My app allows users to set a number of reminders in the future. When the app lauches I want to know what reminders (notifications) have already been set.
我的应用程序允许用户在未来设置一些提醒。当应用程序启动时,我想知道已经设置了哪些提醒(通知)。
Can I read back the notifications I have set or do I need to store in my app (e.g. Core Data or Plist)?
我可以回读我设置的通知吗?我是否需要存储在我的应用程序中(例如 Core Data 或 Plist)?
回答by Scott Berrevoets
UIApplication
has a property called scheduledLocalNotifications
which returns an optional array containing elements of type UILocalNotification
.
UIApplication
有一个名为的属性scheduledLocalNotifications
,它返回一个包含类型元素的可选数组UILocalNotification
。
UIApplication.shared.scheduledLocalNotifications
回答by Frizzo
For Swift 3.0 and Swift 4.0
对于 Swift 3.0 和 Swift 4.0
don't forget to do import UserNotifications
不要忘记做 import UserNotifications
This is working for iOS10+and watchOS3+since the class UNUserNotificationCenteris not available for older versions (link)
这适用于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)
}
}
}
回答by Comradsky
Scott is correct.
斯科特是对的。
UIApplication
's property scheduledLocalNotifications
UIApplication
的属性scheduleLocalNotifications
Here's the code:
这是代码:
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
}
}
For more info, check out this: scheduledLocalNotifications example UIApplication ios
回答by siburb
@Scott Berrevoets gave the correct answer. To actually list them, it is simple to enumerate the objects in the array:
@Scott Berrevoets 给出了正确答案。要实际列出它们,枚举数组中的对象很简单:
[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];
回答by Jim Wilson
Swift 3.0.2:
斯威夫特 3.0.2:
UIApplication.shared.scheduledLocalNotifications
回答by gabriel_vincent
In iOS 10
, using the new UserNotifications
framework:
在iOS 10
,使用新UserNotifications
框架:
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
print("Requests: \(notificationRequest)")
}
回答by Jose Ramirez
Swift 4
斯威夫特 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.
}
}
})
I used this to fix a bug where the same weekly notification was already set and being set again when the app would open, so it would keep resetting the timer to appear, which means it never did appear.
我用它来修复一个错误,即已经设置了相同的每周通知并在应用程序打开时再次设置,因此它会不断重置计时器以显示,这意味着它从未出现过。
回答by Dave G
In Swift, to see all your currently scheduled local notifications printed in the console:
在 Swift 中,要查看在控制台中打印的所有当前计划的本地通知:
print(UIApplication.sharedApplication().scheduledLocalNotifications)