Pages

Wednesday, July 20, 2011

[Code Snippet] Scheduling local notifications

Sometimes we want to create an app that notifies the user some information when a selected date/time is reached, for that reason Apple included local notifications since iOS 4.1

The use of local notifications is much easier than PUSH notifications, that needs to implement a server-side web app for sending PUSH notifications through Apple Push Notification (APN) service.

To use local notifications, you only have to instantiate NSLocalNotification class, set it's fire date and time, its message, sound and any other fields that you could need and schedule it in local notification scheduler, and iOS do the rest for you.

Here is a sample code

UILocalNotification *localNotif = [[UILocalNotification alloc] init];

if (localNotif == nil)
     
return;

localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];

// Set the action button
localNotif.alertAction = @”View”
//This sets the alert text to the current text in the textfield.

localNotif.alertBody = @"Charle's birthday";

//This sets the sound name of the notification.
localNotif.soundName = UILocalNotificationDefaultSoundName;

//You can also set the badge number of the application.
localNotif.applicationIconBadgeNumber = 1;

// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

[localNotif release];

I hope this could be useful for you, enjoy it!!

No comments:

Post a Comment