swift - UTC 时间重复发生的本地通知

标签 swift datetime uilocalnotification

我查看了整个堆栈溢出,只能找到特定时间重复出现的本地通知。但是,我正在尝试查找有关如何根据 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 时间的通知。

关于swift - UTC 时间重复发生的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57929458/

相关文章:

ios - Metal 计算着色器 : setting alpha to zero does not yield complete transparency

python - Matplotlib:在堆叠水平条形图的 x 轴上格式化时间

matlab - 在 MATLAB 中从 CSV 文件中读取日期和时间

ios - 应用关闭时UILocalNotification是否被取消?

ios - 按与当前位置的距离对数组进行排序

swift - Swift 中具有多个条件的 If 语句

swift - 使用计时器在后台刷新应用程序

python:使用时间戳列将时间段分配给行

iOS 8自定义闹钟使用uilocalnotification并在手机静音时在后台播放声音

ios - 在应用商店提交应用时本地通知不起作用