Local notifications :

iPhone Local Notifications_l-min

Local notifications are used to notify user that application is having some updates,

while application is not running in foreground. We can display an alert message or badge on application icon. Local notifications can play sound when alert message or badge is shown.

We can use local notification to remind our daily or monthly tasks at particular time like water plants, pay electricity bill, attend seminar , prepare for presentation etc. In these cases we can use local notification.

Create notification :

- (void)scheduleNotification {
[[UIApplicationsharedApplication] cancelAllLocalNotifications];

UILocalNotification notif = [[cls alloc] init];

notif.fireDate = [datePickerdate];
notif.timeZone = [NSTimeZonedefaultTimeZone];

notif.alertBody = @"Reminder for you";
notif.alertAction = @"Show";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;

NSDictionary *userDict = [NSDictionarydictionaryWithObject:@"Notification 	  text"forKey:kRemindMeNotificationDataKey];

notif.userInfo = userDict;

[[UIApplicationsharedApplication] scheduleLocalNotification:notif];
[notif release];
}

Cancel any exiting local notification

[[UIApplicationsharedApplication] cancelAllLocalNotifications];

Allocate and initialize UILocalNotification object

UILocalNotification *notif = [[cls alloc] init];

Set fire date and timezone. Fire date is date and time when system should deliver the notification. Date is calculated depending on timezone specified.If we do not specify timezone then it will take GMT as default time zone.

notif.fireDate = [datePickerdate];

notif.timeZone = [NSTimeZone defaultTimeZone];

We can control alert message displayed when notification is delivered by setting alertBody and alert action by setting alertAction.

notif.alertBody = @"Reminder for you";

notif.alertAction = @"Show";

We can also specify sound to be played when notification is delivered by setting soundName and also specify badge number on application icon by setting applicationIconBadgeNumber.

 

notif.soundName = UILocalNotificationDefaultSoundName;

notif.applicationIconBadgeNumber = 1;

We can also pass custom data with notification by adding it into dictionary. We can add dictionary to notification using userInfo property.

 

NSDictionary *userDict = [NSDictionarydictionaryWithObject:@"Notification text"forKey:kRemindMeNotificationDataKey];

notif.userInfo = userDict;

Here we are passing notification text as custom data.

In case if we want to increase icon badge number we can get current badge number by using.

[[UIApplicationsharedApplication] applicationIconBadgeNumber ];

If you want to repeat notification then you can set following property to required calendar unit.

notif.repeatInterval = NSMinuteCalendarUnit;

OR

notif.repeatInterval = NSHourCalendarUnit;

OR

notif.repeatInterval = NSDayCalendarUnit;

OR

notif.repeatInterval = NSWeekCalendarUnit;