Мартин Иванов Мартин Иванов - 10 months ago 88
Swift Question

Scheduling weekly repeatable local notification with fire date from date picker in Swift 3

So I'm currently building a schedule app and I'm trying to create a local notification to fire on specific time on specific day of the week, every week. So the first thing I do is get the date value of the start time of the event, then I subtract 5 minutes from the start time value and then schedule the notification. Previously it was very easy just had to type:

notification.repeatInterval = CalendarUnit.WeekOfYear
but now the command is deprecated in Swift 3 and yeah, so the only way I found is:

let someMinutesEarlier = Calendar.current.date(byAdding: .minute, value: -5, to: startTimePicker.date)

let contentOfNotification = UNMutableNotificationContent()

let interval = someMinutesEarlier?.timeIntervalSinceNow

contentOfNotification.title = "Event starting"
contentOfNotification.body = "Some notes"
contentOfNotification.sound = UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval!, repeats: true)
let request = UNNotificationRequest.init(identifier: notificationIdentifier, content: contentOfNotification, trigger: trigger)

let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error as Any)

but this only schedules the notification only once (no matter that the repeat boolean is set to true), because of the year in the someMinutesEarlier...or it's may be something else? Any idea?

Answer Source

As McNight mentionned, you can use UNCalendarNotificationTrigger like so:

let alarmTime = Date().addingTimeInterval(60 * 60 * 24 * 7 - 300) // One week minus 5 minutes.
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: alarmTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

(I haven't tested this code, but this should get you on the right path).

More information here.

Edit: Fixed time interval calculation suggested by SwiftyCruz.