UTC 时间的重复本地通知

Reoccurring local notification for UTC time

我查看了整个堆栈溢出,只能找到特定时间重复出现的本地通知。但是,我正在尝试查找有关如何根据 UTC 时间进行重复本地通知的信息。当您设置重复通知时,它会停留在特定时间的原因。

例如,目前 00:00:00 UTC 是东部时间下午 5 点,但当几个月后夏令时到来时,新时间将为下午 4 点。但是,重复出现的通知仍设置为下午 5 点。因此,由于夏令时,此通知现在关闭一小时。

我正在尝试弄清楚如何完成此操作,以便本地通知能够随着夏令时正确移动。我不确定这是否可行,因为重复发生设置为特定时间,但我很想找到这方面的更多信息。

默认情况下,当前时区被视为传送本地通知。要在注册本地通知时使用 UTC 时间,您需要将时区设置为 UTC。

import UIKit
import UserNotifications

//get the notification center
let center =  UNUserNotificationCenter.current()

//create the content for the notification
let content = UNMutableNotificationContent()
content.title = " Title"
content.subtitle = "SubTitle"
content.body = "jvsvsvasvbasbvfasfv"
content.sound = UNNotificationSound.default

var dateComp = DateComponents()
dateComp.timeZone = TimeZone(identifier: "UTC") // set time zone to UTC
dateComp.day = 1;
dateComp.hour = 08;
dateComp.minute = 00;

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComp, repeats: true)

//create request to display
let request = UNNotificationRequest(identifier: "ContentIdentifier", content: content, trigger: trigger)

//add request to notification center
center.add(request) { (error) in
    if error != nil {
        print("error \(String(describing: error))")
    }
}

以上代码设置每天早上 8 点 UTC 时间通知。