I want to fire an UILocalNotification every 15 minutes. That is no problem:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:15];
notification.repeatInterval = NSMinuteCalendarUnit;
notification.alertBody = NSLocalizedString(#"Alert", #"");
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
But I would like to do that only between 8am and 10pm. How can I handle that?
You will have to use repeaInterval, you probably think of repeat the notification every 15 min, but you have a problem if you want to skip hours.
You could also schedule all notifications for one day (add one for 7:00am, 7:15am, 7:30am, etc) and repeat them for an interval of 1 day. If you want 4 notifications per hour, from 7am to 10pm that makes 60 notifications (which is under the max 64).
Related
I am trying to embed local notification in my code which i want to get repeat after every 15 minutes. It takes NsCalendarUnit and i am unable to figure out as in how to convert 15 mints into NSCalendarUnit
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
localNotification.repeatInterval = 900.0;
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
The repeatInterval is not a time, it an value of NSCalendarUnit.
This means you can NOT set the interval to 15 min.
You best option is to use 4 notification with a repeatInterval of NSCalendarUnitHour and set them 15 minutes apart.
In our app, if a user has set a reminder from us for the future, we set a local notification via,
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] addTimeInterval: 60 * 60]; // adds 1 hour (60 seconds * 60 minutes)
localNotif.fireDate = fireTime;
localNotif.alertBody = #"Alert!";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
Obviously this is easy to test if it's just a few seconds / minutes, but to test 100% when it's scheduled for far in the future, for us to test that, is our only option to just wait?
On Android, you're able to simply adjust the date and time of the phone, and that triggers the local notification, but thus far, I can't see any way to do it on iOS.
Is there a way to?
You need to set the timeZone property of the UILocalNotification. Otherwise, the notification is simply a countdown timer.
localNotif.timeZone = [NSTimeZone systemTimeZone];
I have an app that reminds user about a todo item every day but I do not not need to remind them if they already did it.
My approach to solve the problem is by creating a local notification that is fired up in a daily basis at a random time. When a todo item is already done for the day, I re-scheduled the local notification day to tomorrow by cancelling it and creating a new one dated for tomorrow. But this solution doesn't work. It seems like when you specify that a local notification is fired daily (repearInterval=NSDayCalendarUnit), it doesn't honour the fireDate day property.
Here is my code:
/*** notification is created like this ***/
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [self randomHourMinFromDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"A todo item is due!";
localNotif.alertAction = #"View Item";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.repeatInterval = NSDayCalendarUnit;
/*** some other part of the code ***/
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notification = [scheduledNotifications firstObject];
// adds 1 day to notification fireDate
NSDate *newFireDate = [notification.fireData dateByAddingTimeInterval:60*60*24*1];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
notification.fireDate = newFireDate;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Can you please tell me what I'm doing wrong? Or perhaps recommend a better solution? TIA!
I have a idea. When a todo item is already done for the day, cancel the notification and create a new one. Set firedate to the current date with same time, and set dateByAddingTimeInterval: 60*60*24.
localNotification.fireDate = [CurrentDate dateByAddingTimeInterval:60*60*24];
May this could work.
I want to know if it is possible to add seconds to a local notification? I am trying to create a loop to schedule local notifications 30 seconds after each others. So, in the loop below, can I keep on "delaying" the firedate 30 seconds after each other. I don't know if that question makes sense, but that's the best I can describe my problem as. Think of it as a 30 second interval, but manually scheduling each notification.
for (notifs = 1, notifs <= 64, notifs ++)
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [self.timePicker date];
//can i write it like [self.timePicker date] + 30000?
localNotification.soundName = #"notifsound.caf";
localNotification.alertBody = #"Wake Up!!!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
}
Thanks.
You could use NSDate's dateByAddingTimeInterval: and just pass the current iteration number multiplied by 30.
for (notifs = 1; notifs <= 64; notifs ++)
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[self.timePicker date] dateByAddingTimeInterval:notifs * 30.0];
localNotification.soundName = #"notifsound.caf";
localNotification.alertBody = #"Wake Up!!!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
I want to customize the repeat interval of a UILocalNotification to be on certain days of the week. I haven't found any information about this.
How can I program the repeat interval for notifications for certain days of the week, for example, repeat a notification on Sunday, Monday, and Friday?
Unfortunately you cannot set the repeatInterval property of UILocalNotification to repeat only on particular days. You can set either repeat daily (every day), monthly (every month) or hourly (every hour). So the only feasible solution for your question is that if you want to set an alarm on Sunday, Monday and Tuesday then you have to set 3 alarms (one each for Sunday ,Monday and Tuesday) rather than one alarm.
If you need to custom repeatInterval property. You have to setup each UILocalNotification on specify time. here is my codes.
void (^insertAlarm)(NSDate*fire,NSString*sound,int alarmCount) = ^(NSDate*fire,NSString*sound,int alarmCount){
UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = sound;
notification.fireDate = fire;
notification.repeatInterval = 0;
notification.alertLaunchImage = IMG;
notification.alertAction = ACTION_MSG;
notification.alertBody = BODY;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
};
insertAlarm(date,sound.fileName,0);
insertAlarm([date dateByAddingTimeInterval:60],sound.fileName,1);