Find list of Local Notification the app has already set
Solution 1
UIApplication
has a property called scheduledLocalNotifications
which returns an optional array containing elements of type UILocalNotification
.
UIApplication.shared.scheduledLocalNotifications
Solution 2
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)
}
}
}
Solution 3
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
Solution 4
@Scott Berrevoets gave the correct answer. To actually list them, it is simple to enumerate the objects in the array:
[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];
Solution 5
Swift 3.0.2:
UIApplication.shared.scheduledLocalNotifications
Recycled Steel
Updated on July 23, 2022Comments
-
Recycled Steel almost 2 years
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)?
-
Recycled Steel almost 11 yearsThanks this also help with my next question regarding storing an ID for each notification - all done now...
-
Houman about 10 yearsThis is a strange sample code. The OP is asking for listing the local notifications and you are showing an example how to cancel local notifications
-
Jamshed Alam about 7 yearsHey scott Berrevoests, Please help me out. I am not getting my alarm from second time but is is already in the pending alarm list. stackoverflow.com/questions/44132879/…
-
Shyam about 6 yearsIf I use this at the beginning to check if there are any notifications at all, it doesn't go through the loop at all.
-
ArgaPK about 6 yearsWhich method gets execute when app is in background and receives UNUserNotification?
-
ArgaPK about 6 yearsWhich method gets execute when app is in background and receives UNUserNotification?
-
ArgaPK about 6 yearsWhich method gets execute when app is in background and receives UNUserNotification?
-
ArgaPK about 6 years@ScottBerrevoets Which method gets execute when app is in background and receives UNUserNotification?
-
Matt Le Fleur almost 6 yearsJust a note that
scheduledLocalNotifications
is deprecated -
Henning Hall about 5 years@MattLeFleur is there a new way of doing this? I have trouble finding one.
-
Matt Le Fleur almost 5 years@HenningHall, I'd use Frizzo's answer for the best way of doing this - stackoverflow.com/a/39034576/810167