7

In my app i've to set repetitive UILocalNotification. I'm able to set repeatInterval as daily, weekly etc by doing

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

Ok..it's fine, but i've to set repeatInterval for every 14 days. i came to know from this link that we can only use one of the NSCalendarUnit repeat intervals. So you can have a repeat interval of one minute or one hour or one day but not five minutes or three hours or 14 days. Any Idea about this limitation in iOS 5 or later(that article was written for iOS 4.0)?

Bharat
  • 2,987
  • 2
  • 32
  • 48
  • Some how is it possible to use repeatInterval other than NSCalendarUnit? – Bharat Aug 02 '13 at 10:25
  • Why not resolve this by using a calendar with a 14 day week? Seems like that ought to work. Then the biweekly (7 day week) turns into a simple weekly (14 day) repeat cycle. – Victor Engel Apr 14 '15 at 15:03

3 Answers3

7

You can only set the repeat interval to a calendar unit. In order to get the right time interval you may have to set several notifications though.

If you for instance wanted a notification every 20 minutes you would have to create 3 notifications 20 minutes apart with a repeat interval of NSHourCalendarUnit.

The problem in your case is that the next thing up from the week unit is a month but a month is not exactly 4 weeks.

To actually set a notification for every 14 days you will have to create 26 notifications with a repeat interval of NSYearCalendarUnit.

user2123173
  • 118
  • 1
  • 5
  • thanks for your help but one more limitation of local notification is that we can use a max of 64 notifications only and i've to set repetitive of type daily, weekly, 14 days, monthly etc and here it is a possibility that notification count may cross limit of 64? – Bharat Aug 02 '13 at 10:47
  • The max is a consideration but you have calendar types for daily, weekly and monthly so they will only need one notification each. If you are making use of a weekly, do you also need a 14 day notification? – user2123173 Aug 02 '13 at 10:56
  • yes, infect i need daily, by weekly(2 times a week),weekly, 14 day, and monthly repetition. – Bharat Aug 02 '13 at 11:16
  • Ok.. i think it is the only way to achieve this, thanks for your help – Bharat Aug 02 '13 at 11:23
  • You may want to consider only scheduling maybe a month in advance and then when the app is loaded check if you need to schedule another month in advance. If a user has not opened the app in a month they are probably not interested anymore... – user2123173 Aug 02 '13 at 11:24
  • @Bharat, do you have a link to the documentation where it mentions a limit on notifications please? – James Webster Aug 30 '13 at 13:10
  • Search for "firing 64 notifications" in https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html or see comments in http://blog.mugunthkumar.com/coding/iphone-tutorial-scheduling-local-notifications-using-a-singleton-class/ – Bharat Aug 30 '13 at 13:21
1

You can cancel the notification and set a new one fire date at 14 days from now when handling the notification. i.e. when your app is running in the foreground, do it in:

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

See this SO topic: Set repeatInterval in local notification

I've tried this way, and it works.

Community
  • 1
  • 1
Xiang LI
  • 345
  • 1
  • 9
  • 1
    this callback does not work if the application is in background and user do not dismiss the notification alertBody – hariszaman Dec 07 '16 at 14:55
0

In iOS 10, you can use UNTimeIntervalNotificationTrigger to repeat notification after 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 will help anyone looking for this topic.

Arnab
  • 4,216
  • 2
  • 28
  • 50