In my app I am using UILocalNotifications. I want a functionality in which user can repeat alarms for specific days. Like(Monday,Wednesday,Sunday) at say 9pm. How can i do this.
Thanks in Advance
I am using the following code
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:alertBody];
[notification setFireDate:[myDatePicker date]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = notification.applicationIconBadgeNumber+1;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
There's a repeatInterval property on the noitification , use that.
Err... ...why don't just schedule three notifications for the three days, each with a week as repreatInterval?
Try this code
NSDate *AlarmTime1 =[[NSDate date] dateByAddingTimeInterval:expiretime];
UIApplication *app1 = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm1 = [[UILocalNotification alloc] init];
if (notifyAlarm1) {
notifyAlarm1.fireDate = AlarmTime1;
notifyAlarm1.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval=0;
notifyAlarm.soundName= #"applering_zwnNYFkA.wav";
notifyAlarm1.soundName=#"stop.wav";
notifyAlarm1.alertBody =NSLocalizedString(#"EXPIRE_DESC", nil);
[app1 scheduleLocalNotification:notifyAlarm1];
Related
I need to show certain local notifications while app is in the background. How can I do it, without the help of NSNotificationCenter?
Andrey Chernukha in first comment is right.
Here is the code to implement simple Local Notification:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = 0;
NSDate *now = [NSDate date];
NSDate *dateToFire = [now dateByAddingTimeInterval:10];
localNotification.fireDate = dateToFire;
localNotification.alertBody = #"Test local notification";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I want to set a Local Notification every other day (multiplication on NSDayCalendarUnit), How can I achieve this?
So far, I've done this:
- (void)scheduleLocalNotication {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the fire date/time
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
localNotification.applicationIconBadgeNumber = 1;
// Setup alert notification
[localNotification setAlertAction:#"Open App"];
[localNotification setAlertBody:#"setAreltBody"];
[localNotification setAlertBody:#"You had set a Local Notification on this time"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[localNotification setHasAction:YES];
localNotification.repeatInterval = NSDayCalendarUnit;
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
}
Can I change this line:
localNotification.repeatInterval = NSDayCalendarUnit;
to something like this:
localNotification.repeatInterval = NSDayCalendarUnit * 2;
I guess not, since NSDayCalendarUnit is a typedef, so how can I do this?
I don't think this is possible using the repeatInterval property of UILocalNotification, for the same reason that you already pointed out, NSCalendarUnit is an enum, so you can only use the following values:
An alternative option would be to schedule a number of notifications in a loop and always add 24 hours to the previous fire date.
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.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
//localNotification.fireDate = [NSDate dateWithTimeInterval:timeInterval sinceDate:now];
localNotification.fireDate = [NSDate date];
localNotification.repeatInterval = 5*NSSecondCalendarUnit;
localNotification.alertBody = #"Your alert message";
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
This code generates notification after first 5 second then it goes back to NSMinuteCalendarUnit, I have been trying to work around with this but no help.
I want to set notifications every 5 seconds and then it should fire it until i forcefully want to stop it.
Can somebody please help??
The repeatInterval of a UILocalNotification is of type NSCalendarUnit not an time interval. You can only assign a value from the NSCalendarUnit enum to it.
If you want to fire a local notification every 5 seconds you will need to set multiple notifications.
Here is the code.
Use this for every 5 minutes instead of every week.
-(void)setLocalNotificationwithOptions :(NSDictionary *)launchOptions
{
UILocalNotification *localNoti = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNoti) {
NSLog(#"Notification:%#",localNoti);
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent12 = [gregCalendar12 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:46];
UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:#"ENJOY YOUR WEEKEND!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSMinuteCalendarUnit;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];
}
Hope this helps you. Thank you..
if you want to repeat the timer every 5 seconds,
UILocalNotification allows until 30 seconds per one sound.
So put a 30 seconds sound in your directory,
and then
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDate *date = "set your notification date"
for (int i = 0; i < 10; i++){
NSDate *repeatTime = [date dateByAddingTimeInterval:300 * i];
notification.fireDate = repeatTime;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
I am developing an IOS application with phonegap and need to set local notification for it which will repeat on every friday and specified time
Also there is requirement that user will decide to receive or not the local notification
I recommend you read the following article on the topic which i found very helpful
http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html
- (void)scheduleNotification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Body";
notif.alertAction = #"AlertButtonCaption";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
This is just a basic outline of how it works but starting from this you should be able to schedule a notification.