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
For Swift 3.0 and Swift 4.0
don't forget to do import UserNotifications
This is working for iOS10+ and watchOS3+ since the class UNUserNotificationCenter is not available for older versions (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)
}
}
}
In iOS 10
, using the new UserNotifications
framework:
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
print("Requests: \(notificationRequest)")
}
UIApplication
has a property called scheduledLocalNotifications which returns an optional array containing elements of type UILocalNotification
.
UIApplication.shared.scheduledLocalNotifications
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.
}
}
})
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.
Scott is correct.
UIApplication
's property scheduledLocalNotifications
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
In Swift, to see all your currently scheduled local notifications printed in the console:
print(UIApplication.sharedApplication().scheduledLocalNotifications)