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.
Related
If I set my notification like the code below, it would launch alarm every minute. However, it gets triggered even my alarm is set behind the current time. For example, the current time is 12:00 and the alarm time is 9:00, it still gets triggered because kCFCalendarUnitMinute is a loop.
How can I get the function as my title description?
UILocalNotification *localNotification = [UILocalNotification new];
localNotification.userInfo = #{#"status":#"alarm",#"note":noteString,#"index":#(index)};
localNotification.fireDate = dateToFire;
localNotification.alertTitle = #"Alarm";
localNotification.alertBody = #"Wake Up!";
localNotification.soundName = #"myTone.m4a";
localNotification.alertAction = #"View";
localNotification.repeatInterval = kCFCalendarUnitMinute;
localNotification.regionTriggersOnce = true;
localNotification.applicationIconBadgeNumber =
[UIApplication sharedApplication].applicationIconBadgeNumber + 1;
localNotification.timeZone = [NSTimeZone systemTimeZone];
// To set the local notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
This question already has answers here:
how to set local notification for every two weeks
(2 answers)
Closed 6 years ago.
Good Morning,
How to notify localNotification for every two weeks (14days) a notification.
- (void)applicationDidEnterBackground:(UIApplication *)application {
timer = [NSTimer scheduledTimerWithTimeInterval:60*60*24*14 target:self selector:#selector(getNotifiedForTwoWeeks:) userInfo:nil repeats:YES];
}
-(void)getNotifiedForTwoWeeks:(id)userinfo{
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = #"Notification Message";
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Please let me know is this implementation is correct or not?
Is there any alternative way i can do the best for notifying a LocalNotification message for every two weeks.
Your valuable inputs are appreciated!
When your app will enter to background the system will suspend it after some time and your NSTimer will not schedule.
So you can schedule for every 14 days notification through declare a fireDate and repeatInterval properties in UILocalNotification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateByAddingTimeInterval:60*60*24*14];
localNotification.alertBody = #"Notification Message";
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
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).
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 am trying to schedule a local notificaition that will repeat after every 1 sec once the notification is fired. The notification is fired 10 sec after the application starts.
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [[NSDate alloc]initWithTimeInterval:10 sinceDate:[NSDate date]];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Did you forget something?";
notif.alertAction = #"Show me";
//notif.soundName = UILocalNotificationDefaultSoundName;
notif.soundName = #"applause-light-01.wav";
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSSecondCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Even thought I have used notif.repeatInterval = NSSecondCalendarUnit, notification repeat after 60 sec. What is that I am doing wrong?
notif.fireDate = [[NSDate alloc]initWithTimeInterval:10 sinceDate:[NSDate date]];
This line of code makes your local notification to fire after the new date is created. If you want the notifications right away then you should just create a simple date like this,
notif.fireDate = [NSDate date];
Hope this helps.