Я пытаюсь запланировать серию уведомлений, когда пользователь впервые открывает приложение, поэтому у меня есть следующий код, но проблема в том, что они назначены на 18:30 вместо 19:30.Странно то, что если я планирую только одно уведомление без цикла while, индекс 0 массива работает как 19:30, а остальные нет.Есть идеи, что я делаю неправильно?
Кроме того, если это действительно глупый способ сделать это, пожалуйста, сообщите мне.Мне нужно, чтобы дни были конкретными (таким образом, 1, 3, 5, 7, 14 ... дней с текущего дня).Спасибо.
let triggerDates = [
// Notifications for the first week
Calendar.current.date(byAdding: .day, value: 1, to: Date())!,
Calendar.current.date(byAdding: .day, value: 3, to: Date())!,
Calendar.current.date(byAdding: .day, value: 5, to: Date())!,
Calendar.current.date(byAdding: .day, value: 7, to: Date())!,
// Notifications for the month
Calendar.current.date(byAdding: .day, value: 14, to: Date())!,
Calendar.current.date(byAdding: .day, value: 21, to: Date())!,
// Notifications afterward
Calendar.current.date(byAdding: .day, value: 42, to: Date())!,
Calendar.current.date(byAdding: .day, value: 84, to: Date())!]
var notificationToSchedule = 7
while notificationToSchedule >= 0 {
var dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: triggerDates[notificationToSchedule])
dateComponents.hour = 19
dateComponents.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "notification\(notificationToSchedule)", content: content, trigger: trigger)
center.add(request)
print(trigger.nextTriggerDate() ?? "nil")
notificationToSchedule -= 1
}
Вот что выводит консоль с этим кодом:
2019-06-21 18:30:00 + 0000
2019-05-10 18:30:00 + 0000
2019-04-19 18:30:00 + 0000
2019-04-12 18:30:00 + 0000
2019-04-05 18:30:00 + 0000
2019-04-03 18:30:00 + 0000
2019-04-01 18:30:00 +0000
2019-03-30 19:30:00 + 0000