gpt4 book ai didi

ios - UILocalNotification repeatInterval 中断 timeZone 和 fireDate

转载 作者:行者123 更新时间:2023-11-28 08:05:16 24 4
gpt4 key购买 nike

我在我的应用程序中安排了一批通知,一些具有重复间隔,另一些只是在特定日期触发一次。我正在使用此方法设置我的通知以创建通知:

func notification(date: Date, repeatUnit: NSCalendar.Unit?) -> UILocalNotification {
let notification = UILocalNotification()
notification.category = "ReminderCategory"
notification.alertTitle = "Test"
notification.alertBody = "Test Body"
notification.soundName = "Sound1.m4a"
notification.fireDate = date
notification.repeatInterval = repeatUnit ?? NSCalendar.Unit(rawValue: 0)
notification.timeZone = TimeZone.init(secondsFromGMT: 0)!

return notification
}

如果 repeatUnit 变量设置为任何 NSCalendar.Unit 单位,通知将在正确的时间(对于本地时区)触发。

但是,如果我不设置 repeatInterval,通知 fireDates 会以某种方式设置在过去,并且它们会在我安排通知时立即触发。

有人知道发生了什么事吗?

最佳答案

这对我有用。

    let center = UNUserNotificationCenter.current()

let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Your text"
content.categoryIdentifier = "reminder-notification"
content.sound = UNNotificationSound.default()


var dateComponents = DateComponents()
dateComponents.hour = Calendar.current.component(.hour, from: date)
dateComponents.minute = Calendar.current.component(.minute, from: date)

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request)

关于ios - UILocalNotification repeatInterval 中断 timeZone 和 fireDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45353885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com