I have an array containing my dates. I want to schedule notifications for those days at 6.30am.
I followed the appcoda tutorial which helps scheduling the notification upon input from a datepicker which is great, but I am a bit uncertain on how to call my function to schedule the notification for only the given days.
So my question is how and where to call the function?
- the days are consecutive days
- can I give the function a start date and repeat it with the number of items 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)")
}
}
}