Local notifications iOS example

Notifications: Notifications are similar to reminders or alerts. iOS supports this feature. there are two type of notifications.

  • Push notifications
  • Local notifications

Push Notifications: iOS supports push notifications(APNS) from iOS3.0. iPhone requires internet connectivity to work this.  As notifications will be coming from remote server.
Local Notifications: iOS supports Local notifications from iOS4.0. As the name says, these notifications are local to the iPhone and it doesn’t require any internet¬† connection.

Lets see more on local notifications iOS example and how can we implement these local notifications in iPhone.
We will create a simple app which fires a local notification after 10 secs of a button click. Lets a create a button and
add below code on the button click event.


How it looks on the Device:

Local Notifications iOS example

To send the local notification, we need to set the fire date of when to fire/send the notification to the application.
we need to set time, message to the notification. there are some optional properties to set for the notification.

UILocalNotification* local = [[UILocalNotification alloc]init];
if (local) 
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
local.alertBody = @"Hey this is my first local notification!!!";
local.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:local];


first we need to create the UILocalNotification instance by using alloc and init as given above. Once allocation is success, we need to
set the fireDate, message and timezone properties.

Properties need to set for the local Notifications:
fireDate: When exactly you want fire/send the notification. There are many ways to set this. Here I am using very simple
way to set alert to send 10 sec from current time.
AlertBody: When sending the notification, we need to set some meaningful message related the remainder like ‘Its time to wake up’ ;-).
timeZone: This field is optional, but its good practice to set the timezone as there are different timezones.

After setting the properties, we need to link or add this local notification to the application. If we miss this step,
application don’t know about the notification details. So we need to add this to the application.

Now we are fine with creating local notification and sending to the application. When the application receives this,
how to display or inform. there are two scenarios

Application running on Background:This is the default option for the most of the cases. if you specify the message,
that message will be shown as a alert banner at the top of the home screen.
Application running on Foreground: If the application is in the foreground or currently running, most of the local notifications
ignored by default. If we want to show some alert message, didReceiveLocalNotification delegate method we need to add in appDelegate.m file

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) 
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:notification.alertBody	
						delegate:self cancelButtonTitle:@"OK"
	[alert show];



Download the source:

    All posts by Chandra Pasumarthi