Using Local Notifications in iOS7

Local notifications are a way for an application that isn’t running in the foreground to let its users know it has information for them. Local Notifications can display an alert message or they can badge the application icon. When users are notified they can launch the application and view the details. In this tutorial we will create a local notfication in the form of a message and a badge  on the application icon.

Open Xcode and create a new Single View Application. For product name, use iOS7LocalNotificationsTutorial and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices. 

Go to the Storyboard and drag a Button to the Main View and give it a title of "Send Local Notfication". The Storyboard should look like this.

Select the Assistant Editor and open ViewController.m. Ctrl and drag from the Button to the @interface section and create the following Action.

In ViewController.m implement the sendNotification method

- (IBAction)sendNotification:(UIButton *)sender
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = @"new Blog Posted at";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Here we initialize the Local Notification object and we assign values to the following properties.

  • fireDate - The time when the notification is delivered. We set it here on 5 seconds.
  • alertBody - The message displayed in the notification alert.
  • timeZone - The time zone of the notfication's fire date
  • applicationIconBadgeNumber - The number to diplay on the icon badge. We will increment this number by one.

The notification is then scheduled with the scheduleLocalNotfication method. Go to AppDelegate.m and change the application:didFinishWithLaunchingOptions to

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
application.applicationIconBadgeNumber = 0;

return YES;

If the application isn't running the user will see a banner displayed and a badge. By tapping on action button of the notification, users will launch the app. In this case, this method is called.If a Local Notification is available to process, we will reset  the badge number. Next, add the application:didReceiveLocalNotification method.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
application.applicationIconBadgeNumber = 0;

If the app is running while the notification is delivered, there is no alert displayed on screen. The application automatically calls this method.Again, the badge number is reset. Build and Run the project, press the Local Notification button and the press the Home button. You should see a banner displayed and an increment of the  badge number of the application icon. Click on the banner to go back to the application and to reset the badge number.

You can download the source code of the iOS7LocalNotificationsTutorial at the ioscreator repository on github.