UILocalNotification Example Project Using iOS4

One of the greatest new features that is included in iOS4 is the power that developers have to deliver local notifications.  At the time that the first beta was released in April I was writing specs/requirements and project timeline for a potential app which would have ended up taking me about 5 months to develop.  A large part of the project schedule dealt with having to setup/maintain user reminder preferences...the number of reminders, frequency of each one, time zones, etc.  I tried to think of ever solution that I could that didn't involve the server component, but there really wasn't any other way.

UILocationNotification to the rescue.  After looking over the API docs, Apple had provided exactly what I needed and I was able to cut out 2.5 months from the project schedule because of it.

I was able to create an POC app using UILocationNotifications in literally 5 minutes.  It involved two easy steps:


  1. In the app delegate class I added the following method to verify that the event was fired off if the app was running. -(void)application:(UIApplication *)application didReceiveLocationNotification:(UILocationNotification *)notification;
  2. In the -(void) viewDidLoad method of my controller you alloc/init a new UILocationNotification object, set the fireDate which is an NSDate object, what time zone you want, what the notification message(body) should be and then add the UILocationNotification object to the UIApplication scheduleLocalNotification method


There are two possible end results.  The first being that the event is fired off while the app is running in which case you will not see anything. Hence, why I added the NSLog to the didReceiveLocationNotification method.  The second is if you close the app before the notification has fired and in that case you will receive the alert box with your message.

Download XCode Project

Note: This was compiled with iOS4 GM and tested on iPhone 3G/3GS

22 responses
hey thanks for this one...will this work on Iphone 3G running on IPhone OS 4. IPhone 3G does not support multitasking, and this local notification was listed under multitaksing
Hi, a good example. Does this notification get displayed if the app is not running? something similar to the Messages app where the message is displayed when it appears but the app is not running.


Yes the notification is displayed if the app isn't running.  Just ran this in the simulator again to triple check for compatibility with 4.2.1 and everything was fine.
If the app is running then there is an output, via NSLog, to the debugger console.  Alternatively you could add an alert inside the app's delegate method as well.

It did work. Very useful blog!
Thank you.
Thanks Daniel.
If you have any other comments or suggestions then please let me know.

In a local notification, i would like to display only the action 'View' button and the cancel 'Close' button should not be displayed at all. Basically I want the user to open the app always when a notification is displayed. Is this possible at all?
Thanks for the post,
but do yourself a favor and release the notification if you dont need it anymore ;)

[app scheduleLocalNotification:notification];
[notification release];

Good catch.  I will fix that and update the project on github.

Cory D. Wiles
www.corywiles.com | twitter.com/kwylez

On Tuesday, April 26, 2011 at 8:51 AM,
Thank you for this bare bones example. It is helpful to see such a simple example. However, when I run the app the notification is fired off 3 times and if the app is open application:didReceiveLocationNotification is hit 3 times. Is this expected behavior? I am running the app with the iPhone 4.3 Simulator.
I will look into this

Sent from my iPhone
@Gi-lo fixed that and pushed to github. thanks

@jessica...I verified the nslog that you made note of and not only for 4.2, but 4.0 and 4.3. I am going to look into that.

@Jessica this bug has been fixed and push to github
It's amazing...
It is a great help to implement the local notification for newbie like me.
Can I make it like multiple notifications without clearing the previous assigned notifications?
You should be able to by removing the following lines:
NSArray *oldNotifications = [app scheduledLocalNotifications];      if ([oldNotifications count] > 0) {     [app cancelAllLocalNotifications];   }      if (notification == nil)     return;
Thanks a lot, that was exactly what I was looking for. Cheers
@Phil thank you! If there is anything else you would like to see please let me know.
Thanks for the tutorial Cory!

Im having a play with the "Regions" code template from the apple developer website.
Could you possibly help me out with implementing a local notification for when the user exits the region boundary ??

Thanks again :)

heres the link to the template: https://developer.apple.com/library/ios/#samplecode/Regions/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010726-Intro-DontLinkElementID_2

Local notifications with region boundaries would be a pretty cool feature. I'll have to look more into it. Have you started? Is there a particular part of the app that you are having trouble with?
--  Cory D. Wiles Sent with Sparrow
Hey Cory
Thanks for getting back to me.
Yes i've come up with a, rather crude, but a solution nonetheless:

So if you check out the "Regions" template, in the viewcontroller.m file there is this statement:

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {...

I have added the following (indicated by //comments):
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSString *event = [NSString stringWithFormat:@"didExitRegion %@ at %@", region.identifier, [NSDate date]];
[self updateWithEvent:event];
//implement local notification:

UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];

if (notification == nil)
notification.alertBody = [NSString stringWithFormat:@"Did You Lock Your House?"];
notification.alertAction = @"Lock House";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[app presentLocalNotificationNow:notification];

[notification release];

// ends here
//following is an alert for the case of exiting boundary whilst app is in foreground

UIAlertView *alr=[[UIAlertView alloc] initWithTitle:@"Reminder didExitRegion" message:region.identifier delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];

[alr show];

[alr release];

//ends here


So when the user exits the region, the phone alerts or notifies when this happens...

The issue I'm having now is, simulating this. It works fine when i set a 'custom location' i.e home: since i want to simulate firing a notification when the user walks away from the house. Then change the location to Apple HQ.
But, if set my home custom location, set the region boundary (radius 25m) then set another location (the end of the street) outside the region boundary, nothing happens, the app doesn't even log didExitRegion. I've tried changing the accuracy to best and the filter off etc, no luck in either foreground or background mode.
Doesn't seem to work when i deploy the app onto my iPhone and walk up the street either :(
Would you be up for working together to derive a solution to this? part of my university project :)
Sorry for the delay. Just got back in town. I'll look at this tomorrow during the day and get back with on what the problem might be.

hi Daniel, I wanna use this uilocalnotification for scheduling, i wanna trigger notification at the particular time, Please help me in this