0

I want to run UILocalNotifications and repeat it every 2 minutes. For this I do:

let reminderNote: UILocalNotification = UILocalNotification()
reminderNote.fireDate = NSDate(timeIntervalSinceNow: 60 * 2)
reminderNote.repeatInterval = NSCalendarUnit.Hour
reminderNote.alertBody = "some text"
reminderNote.alertAction = "View"

UIApplication.sharedApplication().scheduleLocalNotification(reminderNote)

It runs it just one time, later it does not.

I think it's because of this line:

reminderNote.repeatInterval = NSCalendarUnit.Hour

How can I repeat my notifications every 1.5 or 2 hours?

John Doe
  • 811
  • 4
  • 14
  • 26
  • Possible duplicate of [UILocalNotification Repeat Interval for Custom Alarm (sun, mon, tue, wed, thu, fri, sat)](http://stackoverflow.com/questions/6966365/uilocalnotification-repeat-interval-for-custom-alarm-sun-mon-tue-wed-thu-f) – Oleg Gordiichuk Feb 04 '16 at 09:12
  • @OlegGordiichuk those post did not help me, so that's why I've opened a new question – John Doe Feb 04 '16 at 09:13
  • You could maybe schedule the notification and within the method that it calls, schedule another one... – Byron Coetsee Feb 04 '16 at 09:38

2 Answers2

1

Directly you can not.

Unfortunately repeatInterval can Only be set as TimeUnit Such as Hour, Minute, Second etc.

Eg: Lets say if you want to repeat notification in each 2 minutes, You will need to create 30 notifications that repeats hourly.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
  • it's not useful =/ What else you can recommend me? I think using Push Notifications not useful in my case. I need to show notifications every some hours set by user. So it can be every hour, 2 hours, 5 hours, etc. – John Doe Feb 04 '16 at 09:17
  • No other option that I am aware of. You'll need to manage your code this way only. – rptwsthi Feb 04 '16 at 09:21
0

firedate sets the time that the notification fires the first time, and repeatInterval is the interval between between repetitions of the notification.

Unfortunately, you can only schedule notifications to repeat at exact intervals defined by NSCalendar constants: e.g., every minute, every hour, every day, every month, but not at multiples of those intervals.

Luckily, to get a notification every 2 minutes, you can just schedule 29 notifications: one right now, one 2 minutes from now, and later one 2 minutes from previous one - up to your 29 notification schedules, and have all repeat every hour. Like so: So the code in the question schedules a notification to fire every 2 minutes (60 * 2 seconds) from now, and then repeat every hour.

UILocalNotification *reminderNote = [[UILocalNotification alloc]init];
reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 2];
reminderNote.repeatInterval = NSHourCalendarUnit;
reminderNote.alertBody = @"some text";
reminderNote.alertAction = @"View";
reminderNote.soundName = @"sound.aif";
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];

reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60];
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];  

For 2 hours, you set your notification for now, after 2 hours from now and 2 hours from the previous one up to 11 notification (12*2 hours) = 24 - 2 hour (because 1st one will be on the day change) = 22/2 = 11 notifications will be required to set on the repeat interval of day by NSDayCalendarUnit.

Kiran Jasvanee
  • 6,362
  • 1
  • 36
  • 52