Swift:本地通知提前 1 小时安排
Swift: Local Notifications get scheduled 1h earlier
我试图在用户首次打开应用程序时安排一系列通知,因此我有以下代码,但问题是它们被安排在 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
这与夏令时有关,因为它在 2019 年 3 月 31 日发生变化。此外,如果您放置一个断点并检查 triggerDates 数组的内容,您将看到实际日期是什么样的,并且只是打印到控制台导致了您的问题。
如果您在 2019 年 3 月 30 日的 BST 中添加 1 天,由于夏令时,您实际上只在 UTC 中添加 23 小时。但是如果你把它转换回来就可以了。
我试图在用户首次打开应用程序时安排一系列通知,因此我有以下代码,但问题是它们被安排在 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
这与夏令时有关,因为它在 2019 年 3 月 31 日发生变化。此外,如果您放置一个断点并检查 triggerDates 数组的内容,您将看到实际日期是什么样的,并且只是打印到控制台导致了您的问题。
如果您在 2019 年 3 月 30 日的 BST 中添加 1 天,由于夏令时,您实际上只在 UTC 中添加 23 小时。但是如果你把它转换回来就可以了。