Sunday, February 20, 2011

Working with UILocalNotification in iOS

Back was the time before iPhone OS 3.0, APNS (apple push notification) was introduced. APN's are the means to send a remote notifications to a device connected to the internet.


With the increasing need of scheduling reminders at the device level, Local notification was introduced with the release iOS 4.0 . With the help of local notification, we can schedule a notification for any required date-time and the notification is handled by the device itself, no need to be connected to the
internet or a remote server.






UILocalNotification is the class behind the local notifications and the customization of the notification.


Local notification limitation:
Every application is allowed a maximum of 64 local notifications. iOS takes the upcoming 64 scheduled notifications and notifies for these notifications only, rest notifications are ignored.


1) How to registering Local notification:


    // Check for whether device supports local notification
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
       
        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = [datePicker date];
        notif.timeZone = [NSTimeZone defaultTimeZone];
       
        notif.alertBody = @"Whooaa?";
        notif.alertAction = @"Show";    // alert button title (other than cancel)
        notif.soundName = UILocalNotificationDefaultSoundName; (default alert sound name, can be cutomized)
        notif.applicationIconBadgeNumber = 1;
       
        // Associate a dictionary to identify to gather the information about the notification
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"Notification text"
                                                forKey:kRemindMeNotificationDataKey];
        notif.userInfo = userDict;
       
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
    }



This will create a notification with the date selected in "datePicker" control.


In this case application name will be the title and "Whoaaa?" will be the body of the alert message, "Show" will be the title of the action button on alert (other than cancel button).


userInfo is the dictionary you will receive in the delegate method of the local notification. So, you can add your desired information for the notification to the dictionary while creating a notification.




2) How to handle notification delegate:


- This delegate method will be called If user tapped "Show Me" button and the application was running in background or was in active state.

- (void)application:(UIApplication *)application
        didReceiveLocalNotification:(UILocalNotification *)notification {
   
    application.applicationIconBadgeNumber = 0;
    NSString *reminderText = [notification.userInfo
                              objectForKey:kRemindMeNotificationDataKey];

    // Call to a method to handover the notification handling
    [viewController handleReminder:reminderText];
}





- This delegate method will be called If user tapped "Show Me" button and the application was not running in background.

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls) {
        UILocalNotification *notification = [launchOptions objectForKey:
                                UIApplicationLaunchOptionsLocalNotificationKey];
       
        if (notification) {
            NSString *reminderText = [notification.userInfo
                                      objectForKey:kRemindMeNotificationDataKey];

            // Call to a method to handover the notification handling
            [viewController handleReminder:reminderText];
        }
    }
   
    application.applicationIconBadgeNumber = 0;
   
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}




3) How to cancel all local notification:

You can cancel the selected local notification if you have preserved the notification object like,


 [[UIApplication sharedApplication] cancelLocalNotifications:notif];


where "notif" is the notification object(saved/unsaved) .


and you can cancel all the local notifications by calling below,
 [[UIApplication sharedApplication] cancelAllLocalNotifications];




4) Custom alert sound:
To make your local notification make a custom sound, you will have to provide the name of the sound file with extension as .caf . This sound file must be in your application resource bundle.
Below code registers a custom notification sound called "alert.caf".
notif.soundName = @"alert.caf";


Note: Alert sound must have .caf extension.


1 comment:

  1. Could you give me your sample project for local notification

    ReplyDelete