I am trying to add UILocalNotification in a loop with different date like for week. When i add fire date not changing. Below is my code, please help me to fixe this issue. Thanks in advance.
NSDate *now = [NSDate date];
BOOL isTimer = NO;
for(i=1;i<8;i++){
NSDate *date = [now dateByAddingTimeInterval:60*60*24*i];
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
NSLog(@"user date time before firing %@",date);
[localNotification setFireDate:(isTimer?[NSDate getDateFromString:[NSDate getEndDateTime:timeInt]]:date)];
NSLog(@"notification fire date %@",localNotification.fireDate);
//Set the date when the alert will be launched using the date adding the time the user selected on the timer
[localNotification setAlertAction:@"Open"]; //The button's text that launches the application and is shown in the alert
[localNotification setAlertBody:titleHeader]; //Set the message in the notification from the textField's text
localNotification.soundName = UILocalNotificationDefaultSoundName;
[localNotification setHasAction: YES]; //Set that pushing the button will launch the application
[localNotification setUserInfo:[NSDictionary dictionaryWithKeysAndObjects:@"title",titleHeader,@"url",link, nil]];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification = nil;
}
Below is the log
user date time before firing 2015-06-10 05:22:00 +0000
notification fire date 2015-06-10 16:11:14 +0000
user date time before firing 2015-06-11 05:22:00 +0000
notification fire date 2015-06-10 16:11:19 +0000
user date time before firing 2015-06-12 05:22:00 +0000
notification fire date 2015-06-10 16:12:34 +0000
user date time before firing 2015-06-13 05:22:00 +0000
notification fire date 2015-06-10 16:12:38 +0000
user date time before firing 2015-06-14 05:22:00 +0000
notification fire date 2015-06-10 16:12:39 +0000
user date time before firing 2015-06-15 05:22:00 +0000
notification fire date 2015-06-10 16:12:39 +0000
user date time before firing 2015-06-16 05:22:00 +0000
notification fire date 2015-06-10 16:12:39 +0000
The problem is that
isTimer
is true, so you are using the same date over and over; yourdate
variable is different each time thru the loop, but it is never used.