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;
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 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.
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
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];
}