Repeating local notification every 14 days (two weeks)?

In my application, I have to set the repeating UILocalNotification . I can set repeatInterval as daily, weekly, etc.

 UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.repeatInterval = NSDayCalendarUnit; // or any other calendarUnit 

Ok .. everything is fine, but I have to set repeatInterval every 14 days. I learned from this link that we can use only one of the NSCalendarUnit repeat intervals. Thus, you can have a repeat interval of one minute or one hour or one day, but not five or three hours or 14 days. Any idea on this limitation in iOS 5 or later (this article was written for iOS 4.0)?

+6
source share
3 answers

You can set the recurrence interval only on the calendar block. To get the correct time interval, you may need to set up several notifications.

If you, for example, wanted to receive a notification every 20 minutes, you would need to create 3 notifications in 20 minutes with an interval of repetition of NSHourCalendarUnit.

The problem in your case is that the next thing from the unit of the week is the month, and the month is not exactly 4 weeks.

To set up a notification every 14 days, you will need to create 26 notifications with an interval of repetition of NSYearCalendarUnit.

+5
source

You can cancel the notification and set a new fire date 14 days after the notification is processed. that is, when your application is running in the foreground, do this in:

 (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

See this topic: Set repeatInterval in local notification

I tried this way and it works.

+1
source

In iOS 10, you can use UNTimeIntervalNotificationTrigger to re-notice every 14 days.

 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:(14*24*3600) repeats: YES]; NSLog(@"td %@", trigger.nextTriggerDate); UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Something went wrong: %@",error); } else { NSLog(@"Created! --> %@",request); } }]; 

Hope this helps anyone looking for this topic.

0
source

Source: https://habr.com/ru/post/950895/


All Articles