31

Say today is a Monday and it's 1 PM. I want to schedule a weekly local notification from my iOS app starting today at 2 PM. I'd do this:

NSDateComponents *components = [[[NSDateComponents alloc]init]autorelease];
components.weekday = 2;
components.hour = 14;
components.minute = 0;

UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
//then make a UNMutableNotificationContent and UNNotificationRequest and schedule it

But if I want to start it next Monday at 2 PM, how do I skip the first occurrence?

To ask the question another way, how do I schedule a repeating UNCalendarNotificationTrigger starting at some arbitrary time, instead of the first occurrence of the repeat interval?

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • 1
    Because NSDateComponents isn't NSDate-aware (in that it is not aware of a _specific_ date, only day/time relative to the start of a particular component, e.g. year, day, or hour), I don't think this is possible. – brandonscript Jan 03 '17 at 18:14
  • The only way I could think of to hack this, is to create a background function on a timer that executes the creation of the notification trigger _after_ the initial occurrence of 2PM Monday. – brandonscript Jan 03 '17 at 18:20
  • @brandonscript that's a bit horrifying. I could also just not schedule that one and hope they open the app again before next week comes around... – Tom Hamming Jan 03 '17 at 18:42
  • Yes, it's a gross hack. Don't do that LOL! I'd say just leave it and not worry about it. I'd suspect iOS users are accustomed to the behaviour (I sure am). Chances of wanting a repeated reminder starting _next_ week and not being willing to dismiss the extraneous one are slim. – brandonscript Jan 03 '17 at 18:44
  • 1
    @brandonscript it's not a huge deal, for sure. Context is reminding users to do a daily task, but not reminding today if they've already done it today. Feature already shipped behaving that way - now I'm updating to new APIs. I'll file a radar if nothing else. – Tom Hamming Jan 03 '17 at 18:47
  • Can you create just every 7 day notification with start day? (without making it weekly, just repeat every 7 days) – NSDmitry Jan 06 '17 at 15:11
  • @NSDmitry I'm not aware of a way to repeat every n days / weeks, where n > 1...can you provide details? – Tom Hamming Jan 06 '17 at 16:14
  • @TomHamming Try using `- (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)opts` once to create date components. – KrishnaCA Jan 07 '17 at 06:32
  • @KrishnaCA can you clarify / make this an answer? – Tom Hamming Jan 10 '17 at 20:06
  • @TomHamming, this approach doesn't work in the way we want it to be. The best bet is to do it through `UILocalNotification` – KrishnaCA Jan 11 '17 at 19:00
  • 4
    I asked in the WWDC 2018 labs about this, and it's not possible with the current API. But they said it's a reasonable thing to ask for, and it may come in a future update. – Tom Hamming Jun 11 '18 at 14:58
  • 2
    I also asked same in WWDC 2018, they noted and told they don't tell any timeline when it will be available in future update! – Hiren Gujarati Jun 30 '18 at 11:07
  • 1
    It would easily have been possible with today's API if subclassing UNCalendarNotificationTrigger didn't cause adding the notification to fail. Just override nextTriggerDate. No changes to API required. – Nailer Nov 16 '18 at 20:10
  • @Michael no news. I haven't seen any new APIs for this in iOS 13, though I haven't watched any related WWDC 2019 sessions. – Tom Hamming Jul 18 '19 at 23:41
  • Is there any news on these? – Przemex3000 Aug 11 '20 at 08:00

0 Answers0