I want to have 5 local notifications every day and here is the code I used to trigger local notification, everything is cool, but if the first notification comes out, and if I run the APP foreground, than or the other notification will not come out. Please help to keep all the notifications even if I run the APP foreground. 😊
UILocalNotification* n1 = [[UILocalNotification alloc] init];
NSCalendar *c1 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co1 = [c1 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co1 setHour:15];
[co1 setMinute:54];
n1.fireDate = [c1 dateFromComponents:co1];
n1.alertBody = #"3:54";
n1.soundName = #"alarm.wav";
n1.timeZone = [NSTimeZone defaultTimeZone];
n1.alertAction= #"3:54";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
UILocalNotification* n2 = [[UILocalNotification alloc] init];
NSCalendar *c2 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co2 = [c2 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co2 setHour:15];
[co2 setMinute:55];
n2.fireDate = [c2 dateFromComponents:co2];
n2.alertBody = #"3:55";
n2.soundName = #"alarm.wav";
n2.timeZone = [NSTimeZone defaultTimeZone];
n2.alertAction= #"3:55";
[[UIApplication sharedApplication] scheduleLocalNotification: n2];
UILocalNotification* n3 = [[UILocalNotification alloc] init];
NSCalendar *c3 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co3 = [c3 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co3 setHour:15];
[co3 setMinute:56];
n3.fireDate = [c3 dateFromComponents:co3];
n3.alertBody = #"3:56";
n3.soundName = #"alarm.wav";
n3.timeZone = [NSTimeZone defaultTimeZone];
n3.alertAction = #"3:56";
[[UIApplication sharedApplication] scheduleLocalNotification: n3];
UILocalNotification* n4 = [[UILocalNotification alloc] init];
NSCalendar *c4 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co4 = [c4 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co4 setHour:15];
[co4 setMinute:57];
n4.fireDate = [c4 dateFromComponents:co4];
n4.alertBody = #"3:57";
n4.soundName = #"alarm.wav";
n4.timeZone = [NSTimeZone defaultTimeZone];
n4.alertAction = #"3:57";
[[UIApplication sharedApplication] scheduleLocalNotification: n4];
UILocalNotification* n5 = [[UILocalNotification alloc] init];
NSCalendar *c5 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co5 = [c5 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co5 setHour:15];
[co5 setMinute:58];
n5.fireDate = [c5 dateFromComponents:co5];
n5.alertBody = #"3:58";
n5.soundName = #"alarm.wav";
n5.timeZone = [NSTimeZone defaultTimeZone];
n5.alertAction = #"3:58";
[[UIApplication sharedApplication] scheduleLocalNotification: n5];
If your App is in foreground No notification is presented but this below method will be called,
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
}
First you should identify the notification by userinfo attached in it.(if you haven't given, give userinfo to every notification.)
present any alert when you get a call in this method.
Related
I am create a local notification with 10 days and at 15:30 when app exit. Here is a code:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
float day = 10;
NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*day];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: newDate];
[componentsForFireDate setHour: 15];
[componentsForFireDate setMinute:30];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDateOfNotification;
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
When I try change date & time in the my phone to the next day (now + 1), It's still show notification, next day, still show. I want local notification show each 10 days when app exit. Any suggests to fix this problems ?
try this code: your app open after 10 day notification fire and other 1 day fire.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsForFireDate = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate: [NSDate date]];
componentsForFireDate.day = componentsForFireDate.day + 10;
[componentsForFireDate setHour: 15];
[componentsForFireDate setMinute:30];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
NSLog(#"Date: %#",fireDateOfNotification);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDateOfNotification;
localNotification.alertBody = #"This is local notification!";
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
hope this code help you.
After changing the time in your system, please check the simulator time also. initially it not updated.
In simulator settings, General->language & region-> Advanced-> Automatica switch will be on mode. select off and on. then you simulator time will also change
I want to get a random string from an array and then display this everyday at 7am.
Here is the code in my AppDelegate.m
-(void)applicationDidEnterBackground:(UIApplication *)application
{
QuoteTableViewController *quoteVC;
int randomlyGeneratedNumber;
randomlyGeneratedNumber =arc4random()%[quoteVC.arrayOfOurQuotes count];
NSString *stringOfTheDay = [[NSString alloc] init];
stringOfTheDay = [quoteVC.arrayOfOurQuotes objectAtIndex:randomlyGeneratedNumber];
NSLog(#"%#",stringOfTheDay);
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
[componentsForReferenceDate setDay:9];
[componentsForReferenceDate setMonth:11];
[componentsForReferenceDate setYear:2012];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
[componentsForFireDate setHour:7];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.repeatInterval = 0;
notification.alertBody = [NSString stringWithFormat:stringOfTheDay];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
There is a error at this line when i run the code
randomlyGeneratedNumber =arc4random()%[quoteVC.arrayOfOurQuotes count];
There is an array in my other View Controller. I did import the view controller.
EDIT :
Array has strings inside but when imported into app delegate it becomes 0. How to fix this?
You need to implement 'arrayOfOurQuotes' as a property of your App delegate instead of the view controller. The view controller and therefore your array will not be around anymore when the view is gone - which is obviously the case if you have already entered the background.
I'm using location service in background to update my app every day. In background it parses the XML and sets local notification at 18h, but only if parsed data is different then "No warning". But I'm getting every day notification with same alert body even when I shouldn't. This is how I push notifications.
If(![self.city isEqualToString:#"No warning"]){
// We only need 1 notification
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregCalendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponents setYear:[dateComponents year]];
[dateComponents setMonth:[dateComponents month]];
[dateComponents setDay:[dateComponents day]];
[dateComponents setHour:18];
[dateComponents setMinute:0];
UIDatePicker *dd = [[UIDatePicker alloc]init];
[dd setDate:[gregCalendar dateFromComponents:dateComponents]];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.alertBody = [NSString stringWithFormat:#"%# %#",self.date,self.city];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.repeatInterval = 0;
[notification setFireDate:dd.date];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
I have Scheduled my local notification to fire at 4.00 pm and want that notification daily at 4.00 pm but the notification fires before time at 3.44 pm , 3.47 pm etc.
here is the code -
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:self.datePicker.date];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self.datePicker.date];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
[dateComponents setSecond:0.0];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = [NSString stringWithFormat:#"How are you feeling?"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.fireDate = [calendar dateFromComponents:dateComponents];
localNotification.repeatInterval = kCFCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Try this:
NSDate *myDate = [NSDate date]; // Set your time here
myDate = [myDate dateByAddingTimeInterval:1*24*60*60];
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = myDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = [NSString stringWithFormat:#"How are you feeling?"];
notif.alertBody = #"Reminder is set";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Hope it will help you.
I've set up multiple Local Notifications on my AppDelegate for my universal app, 14 to be exact. These notifications have specific times and weekdays when they should fire.
The problem is that all of these notifications fire on a daily basis.
For example, I have a notification set for Saturday morning, but it has been showing up daily.
Please give me a suggestion or direction as to why my notifications are firing daily? I can't seem to find a solution.
Please check out my code, thanks!
AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent setWeekday:2];
[dateComponent setHour:11];
[dateComponent setMinute:44];
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:#"GOOD MORNING!"];
[notification setFireDate:[gregCalendar dateFromComponents:dateComponent]];
notification.repeatInterval = NSWeekdayCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
NSCalendar *gregCalendar1 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent1 = [gregCalendar1 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent1 setWeekday:3];
[dateComponent1 setHour:11];
[dateComponent1 setMinute:44];
UILocalNotification *notification1 = [[UILocalNotification alloc]init];
[notification1 setAlertBody:#"GOOD MORNING!"];
[notification1 setFireDate:[gregCalendar1 dateFromComponents:dateComponent1]];
notification1.repeatInterval = NSWeekdayCalendarUnit;
[notification1 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification1];
NSCalendar *gregCalendar2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent2 = [gregCalendar2 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent2 setWeekday:4];
[dateComponent2 setHour:11];
[dateComponent2 setMinute:44];
UILocalNotification *notification2 = [[UILocalNotification alloc]init];
[notification2 setAlertBody:#"GOOD MORNING!"];
[notification2 setFireDate:[gregCalendar2 dateFromComponents:dateComponent2]];
notification2.repeatInterval = NSWeekdayCalendarUnit;
[notification2 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification2];
NSCalendar *gregCalendar3 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent3 = [gregCalendar3 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent3 setWeekday:5];
[dateComponent3 setHour:11];
[dateComponent3 setMinute:44];
UILocalNotification *notification3 = [[UILocalNotification alloc]init];
[notification3 setAlertBody:#"GOOD MORNING!"];
[notification3 setFireDate:[gregCalendar3 dateFromComponents:dateComponent3]];
notification3.repeatInterval = NSWeekdayCalendarUnit;
[notification3 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification3];
NSCalendar *gregCalendar4 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent4 = [gregCalendar4 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent4 setWeekday:6];
[dateComponent4 setHour:11];
[dateComponent4 setMinute:44];
UILocalNotification *notification4 = [[UILocalNotification alloc]init];
[notification4 setAlertBody:#"GOOD MORNING!"];
[notification4 setFireDate:[gregCalendar4 dateFromComponents:dateComponent4]];
notification4.repeatInterval = NSWeekdayCalendarUnit;
[notification4 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification4];
NSCalendar *gregCalendar5 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent5 = [gregCalendar5 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent5 setWeekday:2];
[dateComponent5 setHour:14];
[dateComponent5 setMinute:59];
UILocalNotification *notification5 = [[UILocalNotification alloc]init];
[notification5 setAlertBody:#"GOOD AFTERNOON!"];
[notification5 setFireDate:[gregCalendar5 dateFromComponents:dateComponent5]];
notification5.repeatInterval = NSWeekdayCalendarUnit;
[notification5 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification5];
NSCalendar *gregCalendar6 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent6 = [gregCalendar6 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent6 setWeekday:3];
[dateComponent6 setHour:14];
[dateComponent6 setMinute:59];
UILocalNotification *notification6 = [[UILocalNotification alloc]init];
[notification6 setAlertBody:#"GOOD AFTERNOON!"];
[notification6 setFireDate:[gregCalendar6 dateFromComponents:dateComponent6]];
notification6.repeatInterval = NSWeekdayCalendarUnit;
[notification6 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification6];
NSCalendar *gregCalendar7 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent7 = [gregCalendar7 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent7 setWeekday:4];
[dateComponent7 setHour:14];
[dateComponent7 setMinute:59];
UILocalNotification *notification7 = [[UILocalNotification alloc]init];
[notification7 setAlertBody:#"GOOD AFTERNOON!"];
[notification7 setFireDate:[gregCalendar7 dateFromComponents:dateComponent7]];
notification7.repeatInterval = NSWeekdayCalendarUnit;
[notification7 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification7];
NSCalendar *gregCalendar8 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent8 = [gregCalendar8 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent8 setWeekday:5];
[dateComponent8 setHour:14];
[dateComponent8 setMinute:59];
UILocalNotification *notification8 = [[UILocalNotification alloc]init];
[notification8 setAlertBody:#"GOOD AFTERNOON!"];
[notification8 setFireDate:[gregCalendar8 dateFromComponents:dateComponent8]];
notification8.repeatInterval = NSWeekdayCalendarUnit;
[notification8 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification8];
NSCalendar *gregCalendar9 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent9 = [gregCalendar9 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent9 setWeekday:6];
[dateComponent9 setHour:14];
[dateComponent9 setMinute:59];
UILocalNotification *notification9 = [[UILocalNotification alloc]init];
[notification9 setAlertBody:#"GOOD AFTERNOON!"];
[notification9 setFireDate:[gregCalendar9 dateFromComponents:dateComponent9]];
notification9.repeatInterval = NSWeekdayCalendarUnit;
[notification9 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification9];
NSCalendar *gregCalendar10 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent10 = [gregCalendar10 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent10 setWeekday:6];
[dateComponent10 setHour:19];
[dateComponent10 setMinute:59];
UILocalNotification *notification10 = [[UILocalNotification alloc]init];
[notification10 setAlertBody:#"FRIDAY NIGHT!"];
[notification10 setFireDate:[gregCalendar10 dateFromComponents:dateComponent10]];
notification10.repeatInterval = NSWeekdayCalendarUnit;
[notification10 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification10];
NSCalendar *gregCalendar11 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent11 = [gregCalendar11 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent11 setWeekday:7];
[dateComponent11 setHour:9];
[dateComponent11 setMinute:59];
UILocalNotification *notification11 = [[UILocalNotification alloc]init];
[notification11 setAlertBody:#"HAVE A GREAT WEEKEND!"];
[notification11 setFireDate:[gregCalendar11 dateFromComponents:dateComponent11]];
notification11.repeatInterval = NSWeekdayCalendarUnit;
[notification11 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification11];
NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent12 = [gregCalendar12 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:58];
UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:#"ENJOY YOUR WEEKEND!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSWeekdayCalendarUnit;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];
NSCalendar *gregCalendar13 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent13 = [gregCalendar13 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent13 setWeekday:7];
[dateComponent13 setHour:19];
[dateComponent13 setMinute:58];
UILocalNotification *notification13 = [[UILocalNotification alloc]init];
[notification13 setAlertBody:#"SATURDAY NIGHT PARTY!"];
[notification13 setFireDate:[gregCalendar13 dateFromComponents:dateComponent13]];
notification13.repeatInterval = NSWeekdayCalendarUnit;
[notification13 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification13];
}
Thanks!
To fire notifications at a specific day of the week at a specific time, the following code should work:
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
[dateComponent setWeekday:2]; // For Monday
[dateComponent setHour:11];
[dateComponent setMinute:44];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:#"GOOD MORNING!"];
[notification setFireDate:fireDate];
notification.repeatInterval = NSWeekCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
The first part computes a date that is either on the next or on the previous monday.
The second part defines the notification with a repeat interval of one week.
It does not matter if the fire date is the previous monday, the "next fire date" will automatically adjusted to the next week. (You can verify that with NSLog(#"%#", notification).)
These lines are causing the notifications to repeat on a daily interval:
notification.repeatInterval = NSWeekdayCalendarUnit;
remove those lines and it should fix your problem. The docs say exactly what this property does:
If you assign an calendar unit such as weekly (NSWeekCalendarUnit) or yearly (NSYearCalendarUnit), the system reschedules the notification for delivery at the specified interval. The default value is 0, which means don't repeat.