Previously, I have worked on local notifications, but I got problem in it now. Suppose today, I have to set a notification for 1 day before date : 07/07/2012 in my app. How can I will be able to do that. I’m setting notification like this..
- (IBAction) scheduleNotification:(id) sender {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
for (int i = 0; i< [delegate.viewController.contactList count] ; i++) {
UILocalNotification *localNotification = [prototypeNotification copy];
NSString *name = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:NAME_KEY];
NSString *birthday = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:BIRTHDAY_KEY];
if (birthday) {
[formatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [formatter dateFromString:birthday];
if (date == nil) {
[formatter setDateFormat:@"MM/dd"];
NSDate *date = [formatter dateFromString:birthday];
}
else {
}
self.alarmTime = [date dateByAddingTimeInterval:10];
localNotification.fireDate = alarmTime;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
NSString *itemName = @"B'day Alert!!!";
NSString *msgName = [NSString stringWithFormat:@"Celebrate %@'s B'day",name];
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:itemName,MessageKey, msgName,TitleKey, nil];
localNotification.userInfo = userDict;
localNotification.soundName = self.soundName;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
else {
// write else part
}
[localNotification release];
}
}
When I encounter NSDate *date = [formatter dateFromString:birthday]; this line , date is not coming accurately,so I’m not able to set local notification properly. If any thing m doing wrong please, help me out.
NSDateFormatter has a different behavior than that of you would think. If you’re confused whether it uses GMT, UTC, the time zone based of our current system locale or something else, read this question and answer: NSDateFormatter return incorrect date from string