Saturday, May 25, 2013

UIApplication presentLocalNotificationNow example in Objective C (iOS)


[UIApplication presentLocalNotificationNow]

Presents a local notification immediately.
- (void)presentLocalNotificationNow:(UILocalNotification*)notification
Parameters
notification
A local notification that the operating system presents for the application immediately, regardless of the value of the notification’s fireDate property. Applications running in the background state can immediately present local notifications when there are incoming chats, messages, or updates. Because the operating system copies notification, you may release it once you have scheduled it.

Example of [UIApplication presentLocalNotificationNow]

-(void)enterRegionNotify
{
    UILocalNotification *note = [[UILocalNotification alloc] init];
    note.alertBody= @"Region Left"; //ToAsk: what should be displayed ? 
    note.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:note];
    [note release];

}

Example of [UIApplication presentLocalNotificationNow]
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState applicationState = application.applicationState;
    if (applicationState == UIApplicationStateBackground) {
        [application presentLocalNotificationNow:notification];
    }
}
Example of [UIApplication presentLocalNotificationNow]
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    ......
    ......
    if (localNotif)
    {
      localNotif.alertBody = [NSString stringWithFormat:
                                                NSLocalizedString(@"%@ has a message for you.", nil), str_friend];
      localNotif.alertAction = NSLocalizedString(@"Read Msg", nil);
      localNotif.soundName = @"alarmsound.caf";
      localNotif.applicationIconBadgeNumber = 1;
      [localNotif setRepeatInterval:NSMinuteCalendarUnit];
      [application presentLocalNotificationNow:localNotif];

      str_friend = nil;
      break;
     }
}