How can I skip the first occurrence of a repeating UNCalendarNotificationTrigger?
Asked Answered
A

1

36

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?

Anhydrous answered 3/1, 2017 at 18:7 Comment(15)
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.Autosome
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.Autosome
@Autosome 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...Anhydrous
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.Autosome
@Autosome 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.Anhydrous
Can you create just every 7 day notification with start day? (without making it weekly, just repeat every 7 days)Medford
@Medford I'm not aware of a way to repeat every n days / weeks, where n > 1...can you provide details?Anhydrous
@TomHamming Try using - (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)opts once to create date components.Distressed
@Distressed can you clarify / make this an answer?Anhydrous
@TomHamming, this approach doesn't work in the way we want it to be. The best bet is to do it through UILocalNotificationDistressed
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.Anhydrous
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!Queue
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.Bonnice
@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.Anhydrous
Is there any news on these?Wellesz
L
0

Thinking outside of the box here.

You could configure your server to deliver a silent push notification to your phone some time in the future (i.e: tomorrow early morning). When you receive the push notification in the background you can use this to setup your local calendar notification. This way you can effectively skip the first notification, or schedule them to start any time in the future.

Of course, you shouldn't rely exclusively on the APNS network, so you'll need to track state of the notifications via an API call when the app is foregrounded.

Labionasal answered 12/12, 2023 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.