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);
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.
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).
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'm working on an application in which i want to show a UILocalNotification for an inconsistent interval. notification will appear on the day of week user want to see i.e. user selects monday, tuesday and friday then notification will be visible on these days of each upcoming week.
Thanks for any help in advance.
I think what you need to do is schedule multiple UILocalNotification instances for the irregular periods in your interval. You can use the repeatInterval property to have each UILocalNotification repeat.
For your example, you could have three UILocalNotification instances scheduled for Monday, Tuesday, and Friday, and then set the repeatInterval value of each of these instances to NSWeekCalendarUnit.
A UILocalNotification takes an NSDate for its fireDate property. Why don't you create an NSDate instance based on feedback from the user through the UI and use that?
To create the NSDate object, you could either simply call initWithTimeIntervalsSinceNow: using a multiple of seconds. For example, to create an NSDate representing the time 6 days and 5 hours from now:
NSDate* futureDate = [[NSDate alloc] initWithTimeIntervalSinceNow:6*24*60 + 5*60];
Or, if you need to take the user's particular calendar (remember there are many different calendars in use around the world) or timezone into account, you should also check out the NSCalendar and NSTimeZone classes. The 'Date and Time Programming Guide' is a great walkthrough of these.
Put On AppDelegate.m and call from enterbackground and terminal method
-(void)localNotification{
UILocalNotification *scheduledAlert;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
scheduledAlert = [[[UILocalNotification alloc] init] autorelease];
NSDate *date = [NSDate date];
NSLog(#"%#",date);
scheduledAlert.fireDate = [NSDate dateWithTimeIntervalSinceNow:86400];
scheduledAlert.timeZone = [NSTimeZone defaultTimeZone];
scheduledAlert.repeatInterval = NSDayCalendarUnit;
NSArray *arr =[[NSArray alloc]initWithObjects:#"Your Notification 1",#"Your Notification 2",#"Your Notification 3",nil];
int jk = arc4random()%arr.count;
scheduledAlert.alertBody = [arr objectAtIndex:jk];
[[UIApplication sharedApplication] scheduleLocalNotification:scheduledAlert];
}
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.