I have an array containing my dates. I want to schedule notifications for those days at 6:30 in the morning.
I followed the appcoda tutorial , which helps to schedule a notification when entering a different date, but I'm a little unsure of how to call my function to schedule a notification only for the specified days.
So my question is: how and where to call the function?
- days in a row.
- Can I give the function a start date and repeat it with the number of elements in the array?
Below is my function:
func scheduleNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: 6, minute: 30)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Advent Calendar"
content.body = "Just a reminder to open your present!"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
Ben s source
share