0

my app has to remind the user of something every n days.

Currently, I have

var dateComponents = DateComponents()
dateComponents.hour = userHour
dateComponents.minute = userMinute
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

My problem is that I don't know how to express the skipping of dates with DateComponents.

I've found UNTimeIntervalNotificationTrigger which allows me to set it off every 24 or 48 etc. hours but this give me the possibility to fire at a certain daytime.

Should I mix those 2? First set the Calendar trigger and in the first notification make it actually repeat? Doesn't seem right to me, there must be an easier way.

user2875404
  • 3,048
  • 3
  • 25
  • 47
  • I think you need to use both. The only issue is that the user must click the first notification, for the `UNTimeIntervalNotificationTrigger` to be scheduled with the correct interval. – Aris Apr 25 '18 at 18:32
  • 1
    Possible duplicate of [Trigger UILocalNotification for every 14 days (fortnightly) Swift](https://stackoverflow.com/questions/46070648/trigger-uilocalnotification-for-every-14-days-fortnightly-swift) – Dávid Pásztor Apr 25 '18 at 18:34

1 Answers1

2

I've looked quite deeply into iOS local notifications and there are some things that are weirdly absent. This is one of those things as far as I know.

If you don't need to set a specific time for the notification to fire you can use the UNTimeIntervalNotificationTrigger which would allow you to set a notification every 72 hours from the time you set it.

If you need and exact time for the notification to go off you could use your code but add dateComponents.weekday and set 1 and 4 or whatever. Which isn't perfect but might work for what you need.

A solution that will take a lot more work would be to create like 5 notifications. Set the day and time you want them to fire (3 days away, 6 days, 9 days, etc) Then when the user launches the app you look to see if any of the notifications have been sent and set them again in the future. These would not be repeating. It's up to you to manually set them each time.

Ian Kohlert
  • 484
  • 1
  • 4
  • 15
  • Thank you for the answer. The 5-notification idea is what I will go for for now, I can live with that. Will tick if there is no other answer stating otherwise. Thank you – user2875404 Apr 25 '18 at 21:34