I want to trigger a UILocalNotification every Sunday at 8PM, however, I'm having a fire date every day at 8PM.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"New updates added for that week!"] forKey:#"new"];
notification.repeatInterval= NSDayCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;
NSLog(#"notification: %#",notification);//it indicates that the notif will be triggered today at 8PM and not Sunday.
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
Thank you.
You missed the NSWeekCalendarUnit in the NSDateComponents init function. Add the NSWeekCalendarUnit to it and set the repeatInterval to NSWeekCalendarUnit, then output is
next fire date = Sunday, November 24, 2013 at 8:00:00 PM
The code is here:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
//...
notification.repeatInterval = NSWeekCalendarUnit;
//...
I have also searched about it. Below code work good for me. Pass the week day value 1 to 7 Sunday to Saturday and notification body with action which you want to fire and specify your date then notification will come on that specific day.
Time will automatically set when you set date.
Hope this help you.
-(void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate];
[componentsForFireDate setWeekday: weekday] ; //for fixing Sunday
// [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
// [componentsForFireDate setMinute:0] ;
// [componentsForFireDate setSecond:0] ;
notification.repeatInterval = NSWeekCalendarUnit;
notification.fireDate=[calendar dateFromComponents:componentsForFireDate];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Related
I have checked the iOS 10 Home app. The screenshot is captured from Home app only.
Since last 2 days I have been trying to implement the HMTimerTrigger repeat functionality. My requirement is I have to repeat the Trigger on every monday,tuesday and friday. What I found is I can add only one day(Monday or Tuesday ... but not Monday AND Tuesday) like below.
unsigned flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
NSDate *fireDate = [NSDate date];
NSDateComponents *recurrenceComponents = [[NSDateComponents alloc] init];
recurrenceComponents.weekday = 2; // For monday
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:flags fromDate:fireDate];
fireDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
HMTimerTrigger *trigger = [[HMTimerTrigger alloc] initWithName:triggerName.text
fireDate:fireDate
timeZone:nil
recurrence:recurrenceComponents
recurrenceCalendar:[NSCalendar currentCalendar]];
Thank you for reading my post. Any ideas/suggestions would be very helpful.
This functionality is not available to the public even though Apple has it in their HomeKit app. The best you could do is create multiple triggers for each day but then the user would gets confused.
Please open up a radar bug on it here
Few years back i did same thing using notification. You cannot create multiple trigger with one instance. So you have to create it for each day. I hope this example may give you some idea.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:[NSDate date]];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: referenceDate];
NSArray *arrayDays = [NSArray arrayWithObjects:#"1",#"3",#"5", nil];
for (int i=0; i<[arrayDays count]; i++)
{
// set components for time 2:00 p.m.
[componentsForFireDate setWeekday:[[arrayDays objectAtIndex:i] intValue]];
[componentsForFireDate setHour: 14];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *firstDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification1 = [[UILocalNotification alloc] init] ;
notification1.fireDate = firstDateOfNotification;
notification1.timeZone = [NSTimeZone localTimeZone] ;
notification1.alertBody = [NSString stringWithFormat: #"Complete your daily survey"] ;
notification1.alertAction = #"go back";
notification1.repeatInterval= NSWeekCalendarUnit;
notification1.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification1] ;
}
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 have a json containing various times. Like below
{
"5:10"
"14:24"
}
I need to use this time to fire local notification everyday.
Can you please suggest me how to properly put this string in this line?
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setHour: 20] ;
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"New updates added for that week!"] forKey:#"new"];
notification.repeatInterval= NSDayCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;
NSLog(#"notification: %#",notification);
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
In this code I am manually adding the time to 8 pm. I need to put that time from json into the code.
I am developing alarm application, i want to do the repeat alarm functionality same as there in iOS Alarm Clock application, where we can select repeat option as Every Monday, Every Tuesday,... likewise.
Example: If i select only Every Monday, Every Wednesday and Every
Friday then for all upcoming Monday, Wednesday and Friday the
respective Alarm Notification should fire.
i tried:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [dictionaryForAlarm objectForKey:kAlarmSnoozeDate]];
NSArray *arrayForRepeatDays = #[#"2",#"4",#"6"];
for (NSString *dayOfWeek in arrayForRepeatDays) {
[componentsForFireDate setDay:[dayOfWeek integerValue]];
localNotification.repeatInterval = NSCalendarUnitWeekOfMonth;;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
but it's not getting fire on the respective days, correct me for this
Thank you in advace!
I think you need to do code like this:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [dictionaryForAlarm objectForKey:kAlarmSnoozeDate]];
NSArray *arrayForRepeatDays = #[#"2",#"4",#"6"];
for (NSString *dayOfWeek in arrayForRepeatDays) {
[componentsForFireDate setWeekday: [dayOfWeek integerValue]]
[componentsForFireDate setHour: 10] ; // here you can set hr as per you need
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.fireDate=[calendar dateFromComponents:componentsForFireDate];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
I Guess this two like make this working:
[componentsForFireDate setWeekday: [dayOfWeek integerValue]]
localNotification.repeatInterval = NSWeekCalendarUnit;
I am New to NSCalander, NSdates, and NSDateComponents
Basically I have a Local Notification and I want to repeat the fire date based on the selection of the user, let's say on Sunday and Monday only.
We should use repeatCalendar property for the UILocalNotification but I couldn't reach how to setup it.
So any One can help me with simple line of codes ?
Thanks
There is a code snippet to set the UILocalNotification to fire at every sunday 20:00.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit| NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"New updates added for that week!"] forKey:#"new"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;
NSLog(#"notification: %#",notification);
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
enjoy