Wednesday 21 November 2012

Local Notification in iPhone


Creating, configuring, and scheduling a local notification


- (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDateComponents *dateComps = [[NSDateComponents alloc] init];

[dateComps setDay:item.day]; [dateComps setMonth:item.month];

[dateComps setYear:item.year]; [dateComps setHour:item.hour];

[dateComps setMinute:item.minute]; NSDate *itemDate = [calendar dateFromComponents:dateComps]; [dateComps release];

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

if (localNotif == nil)
return; 

localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];

localNotif.timeZone = [NSTimeZone defaultTimeZone];

localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
item.eventName, minutesBefore]; localNotif.alertAction = NSLocalizedString(@"View Details", nil);


localNotif.soundName = UILocalNotificationDefaultSoundName; 

localNotif.applicationIconBadgeNumber = 1;

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];

localNotif.userInfo = infoDict;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

 [localNotif release];
}


No comments:

Post a Comment