Schedule notifications with Swift 3 for an array of dates

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)")
            }
        }
    }
+4
source share
1 answer

, @gklka , 24 ), day, , , :

func scheduleNotification(day: Int) {

    var date = DateComponents()
    date.year = 2016
    date.month = 11
    date.day = day
    date.hour = 6
    date.minute = 30

    let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
}

for:

for index in 1...24 {
        scheduleNotification(day: index)
    }

int AppDelegate, didFinishLaunchingWithOptions

1-

, , . 😔. , , . 2 .

  • , , , , - . UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

  • , requestIdentifier, 1 . ID , : let requestId = "textNotification\(day)"

+1

Source: https://habr.com/ru/post/1662399/


All Articles