gpt4 book ai didi

ios - 使用非公历创建 UNNotification

转载 作者:行者123 更新时间:2023-11-29 11:38:21 26 4
gpt4 key购买 nike

我正在构建我的应用程序以在 IOS swift 4 中触发每日、每周和每月的通知。这些通知使用公历完美运行。但是,当我将日期更改为 Hijri 日历 (Calendar(identifier: .islamicUmmAlQura) 它不会工作。它接缝 UNCalendarNotificationTrigger 将任何日期转换为公历。以下每月通知代码在使用公历时完美运行:

func myNotification(at date: Date, withTitle title:String, andBody body:String, notificationIdentifier:String) {

let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.day,.hour, .minute], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.init(named: "notificationSound.wav")
let request = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notificationIdentifier])
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print(" error: \(error)")
}
}

当我使用 (Calendar(identifier: .islamicUmmAlQura) 将日期转换为伊斯兰日期时,以下代码不起作用:

func myNotification(at date: Date, withTitle title:String, andBody body:String, notificationIdentifier:String) {

let calendar = Calendar(identifier: .islamicUmmAlQura)
let components = calendar.dateComponents([.day,.hour, .minute], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.init(named: "notificationSound.wav")
let request = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notificationIdentifier])
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print(" error: \(error)")
}
}

最佳答案

.calendar 添加到组件中,这将使UNCalendarNotificationTrigger 使用您的日历计算日期。

let date = Date()
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.calendar, .day, .hour, .minute], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

关于ios - 使用非公历创建 UNNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47954703/

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