嗨,我想从字符串时间本地通知,我想在给定时间前10分钟通知启动。我有麻烦如何使时间从4:01到3:51,请帮助。这是我的代码
let a = "4:01"
func startNoftification(prayer: String) {
let time = prayer.split(separator: ":").map { (x) -> Int in
return Int(String(x))!
}
let content = UNMutableNotificationContent()
content.title = "Adzan"
content.body = "it's time to sholat dzuhur"
let gregorian = Calendar(identifier: .gregorian)
var component = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
component.hour = time[0]
component.minute = time[1] - 10
guard let date = gregorian.date(from: component) else { return }
let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: "Hijrah", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let err = error {
print("Notif error:", err)
return
}
}
}
2条答案
按热度按时间rta7y2nd1#
这就是你想要的吗?
或者如果你的倒计时时间有很多时间成分,使用
DateComponents
。w1jd8yoj2#
在这里,我有解决方案合并rakesha Shastri的答案,希望它能帮助别人.
首先在appDelegate和控制器中导入UserNotification
并添加didFinishWithLaunchingWithOptions的代码
您可以创建一个函数,并添加来自API或任意字符串的字符串时间参数,因为我没有使用函数,所以我在viewDidLoad中传递了我的解决方案