Implementing Local Notifications in iPhone Applications

Integrating push notifications with iPhone has been really tricky before iOS4.0. You needed to have a dedicated server to push notifications to the applications. However with iOS4.0, Apple introduce the concept of local notificatiions. Local Notifications are a new way to send “push notifications” to your app without having a dedicated server and associated server side coding. Local notifications can be scheduled on the user’s device to fire at any given time; you can even set them to be recurring.The primary purpose of local notifications is to provide a mechanism for a suspended or background application to gain the attention of the user. For example, an audio streaming app might need to notify the user about the loss of network connection or a calendar based application an approaching appointment. The blog details about the coding of delivering a local notification on an iOS device. How To use Local Notification To have iOS deliver a local notification at a later time, an application creates a UILocalNotification object, assigns it a delivery date and time, specifies presentation details, and schedules it. However, make sure you are implementing application:didreceiveLocalNotification in your app delegate if you want to see the notification while your app is in the foreground. Scheduling Notifications
- (IBAction) scheduleAlarm:(id) sender
{
    [eventText resignFirstResponder];

    // Get the current date
    NSDate *pickerDate = [NSDate date];

    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
												   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit )
												   fromDate:pickerDate];
    // Set up the fire time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]]; // Notification will fire in one minute
    [dateComps setMinute:[timeComponents minute]];

    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;// this sets when notification will be called.
    // Notification details
    localNotif.alertBody = [eventText text];// this shows the alert box with message.
    // Set the action button

    localNotif.alertAction = @"View";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    // Specify custom data for the notification

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;
    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
}
In this example we set the notification to the text that the user entered into the text field. InfoDict dictionary is our chance to associate some additional information with the alert. When a local notification is triggered, the option is available to play a sound to gain the user’s attention. If such an audio alert is required the corresponding sound file must be added to the application project resources and must be in Linear PCM, MA4 (IMA/ADPCM), uLaw or aLaw format. If no sound file is specified, the default is for the notification to be silent (though the iPhone device will still vibrate). Handling Notifications After They Fire The last thing is to determine what to do when a notification fires. This step is handled inside of the appDelegate. When a notification fires, there are one of two situations. 1. The app is running and 2. The app is not running (or running in the “background”) . Open up your app delegate .m file and add the following code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   {
  // Override point for customization after application launch.
  // Add the view controller's view to the window and display.

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

  // Handle launching from a notification

    UILocalNotification *localNotif =[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        NSLog(@"Recieved Notification %@",localNotif);
    return YES;
                    }
    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
                    {
  // shows badge value on tab bar
       NSString *badgeValue = [NSString stringWithFormat:@"%d",notifyCount];
       [[[[[self tabBarontroller] tabBar] items] objectAtIndex:2] setBadgeValue:badgeValue];

  //  setting badge value of tab bar as shown below.
       application.applicationIconBadgeNumber = 0;
                    }
The method is invoked when a running application receives a local notification. In this method, we create a string named badgeValue,which contains an integer value from notifyCount variable. We set this badgeValue in the tab bar item which is present at index no 02, which is meetings tab in the provided screenshot.