使用非公历日历创建UNNotification

问题描述 投票:1回答:2

我正在构建我的应用程序以在IOS swift 4中发送每日,每周和每月通知。通知使用公历完美地工作。但是,当我将日期更改为Hijri日历(日历(标识符:.islamicUmmAlQura)时,它不是wrk。它接缝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)")
    }
}

}

当我使用(日历(标识符:.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)")
    }
}
ios time calendar usernotifications
2个回答
1
投票

将日历类型添加到触发器后,它可以工作

trigger = UNCalendarNotificationTrigger(calendar:calendar, dateMatching: components, repeats: true)

0
投票

如果UserNotifications框架期望在特定日历中解释DateComponents,那么这就是您必须使用的日历。这并不意味着基础日期时间将发生变化或不正确。

Date只是时间轴上的一个点。时间线如何划分和命名是日历的函数,但Date本身并不知道或不关心。无论您选择何种描述或分解,时间轴上的点都保持不变。

参看Convert NSDates from one calendar to another

© www.soinside.com 2019 - 2024. All rights reserved.