41

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)?

Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153
Recycled Steel
  • 2,272
  • 3
  • 30
  • 35

8 Answers8

44

UIApplication has a property called scheduledLocalNotifications which returns an optional array containing elements of type UILocalNotification.

UIApplication.shared.scheduledLocalNotifications
Peter
  • 29
  • 1
  • 5
Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • Hey scott Berrevoests, Please help me out. I am not getting my alarm from second time but is is already in the pending alarm list. https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio – Jamshed Alam Jun 06 '17 at 06:54
  • @ScottBerrevoets Which method gets execute when app is in background and receives UNUserNotification? – ArgaPK Apr 18 '18 at 11:57
  • 4
    Just a note that `scheduledLocalNotifications` is deprecated – Matt Le Fleur Aug 01 '18 at 10:49
  • @MattLeFleur is there a new way of doing this? I have trouble finding one. – Henning Hall Jun 26 '19 at 10:49
  • 1
    @HenningHall, I'd use Frizzo's answer for the best way of doing this - https://stackoverflow.com/a/39034576/810167 – Matt Le Fleur Jun 28 '19 at 08:56
37

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)
        }
    }
}
Community
  • 1
  • 1
Frizzo
  • 432
  • 5
  • 10
22

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

SkylerHill-Sky
  • 2,106
  • 2
  • 17
  • 33
  • Thanks this also help with my next question regarding storing an ID for each notification - all done now... – Recycled Steel Jul 09 '13 at 09:33
  • 1
    This 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 – Houman Mar 30 '14 at 12:38
6

@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);
}];
siburb
  • 4,880
  • 1
  • 25
  • 34
4

Swift 3.0.2:

UIApplication.shared.scheduledLocalNotifications
Jim Wilson
  • 1,548
  • 1
  • 9
  • 7
3

In iOS 10, using the new UserNotifications framework:

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

        print("Requests: \(notificationRequest)")
}
gabriel_vincent
  • 1,230
  • 3
  • 16
  • 35
3

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.

Hedylove
  • 1,724
  • 16
  • 26
  • If I use this at the beginning to check if there are any notifications at all, it doesn't go through the loop at all. – Shyam Apr 16 '18 at 06:38
  • Which method gets execute when app is in background and receives UNUserNotification? – ArgaPK Apr 18 '18 at 11:56
1

In Swift, to see all your currently scheduled local notifications printed in the console:

print(UIApplication.sharedApplication().scheduledLocalNotifications)
Dave G
  • 12,042
  • 7
  • 57
  • 83