0

I'm trying to create application that will send local notifications every n days.

I has DailyRepeat structure that contains notification info:

struct DailyRepeat: BaseRepeat {

    var title: String
    var body: String
    var date: Date

    var day: Int

}

And method that schedule notification:

func notifyDaily(at notification: DailyRepeat) {
    let content = generateContent(title: notification.title, body: notification.body)
    let dateComponents = DateComponents(day: notification.day, hour: notification.date.time.hours, minute: notification.date.time.minutes)
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

    notificationCenter.add(request)
}

My first thought was to create UNCalendarNotificationTrigger on first fire date, than handle notification and set UNTimeIntervalNotificationTrigger, but unfortunately I cannot find the way to handle notification receive without user interaction.

Any thoughts how it should work?

Arthur Stepanov
  • 511
  • 7
  • 4

1 Answers1

0

As per the documentation if you want the notification to repeat you need to set repeatable constraints for the date components.

So to set a notification to run every morning at 8:30AM you would just set the hour and the minute and set it to repeat.

var date = DateComponents()
date.hour = 8
date.minute = 30 
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)

I don't think that you can use repeats to repeat every x number of days, you can set specific days of the week, or dates of the month, but not every n days.

You could set a TimeInterval of x number of days and repeat that, but getting the exact time to start it might be tricky.

Scriptable
  • 19,402
  • 5
  • 56
  • 72