Easy way to see (and debug) UILocalNotifications

It doesn’t take a lot of code to create a UILocalNotification but when you have multiple of them and you’re doing a lot of calendar math and things start getting hairy. To demystify that experience I created a project with a UITableViewController that prints out all of the current UILocalNotifications.

We’ll create 3 local notifications 1 minute apart for the next 3 minutes with these calls:

   [self createLocalNoticationAt:[[NSDate date] dateByAddingTimeInterval:60] withTitle:@"Every Minute 1!"];
    [self createLocalNoticationAt:[[NSDate date] dateByAddingTimeInterval:120] withTitle:@"Every Minute 2!"];
    [self createLocalNoticationAt:[[NSDate date] dateByAddingTimeInterval:180] withTitle:@"Every Minute 3!"];

To this function.

-(void) createLocalNoticationAt: (NSDate *)alertDate withTitle: (NSString *) alertTitle {
 UILocalNotification *localNotification = [[UILocalNotification alloc]init];
 // create a calendar
 localNotification.fireDate = alertDate;
 localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = alertTitle;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
 [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

The magic happens in the UITableViewController which shows our UILocalNotifications. We can retrieve all of them (just for that app) in an NSArray with a simple call.

NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];

Each item in the NSArray is a UILocalNotification. The main properties are firedate and alertBody.

I’ve put a full project up on github.