我现在如何调用本地通知?而不是在未来设定一个时间间隔?此代码将在未来 5 秒后显示通知。我希望现在就调用它。
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false)
let notificationRequest = UNNotificationRequest(identifier: "notification", content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
if let error = error {
//error
}
}
最佳答案
因为timeInterval必须大于0,所以可以尝试在timeInterval中放入0.1。
或者您可以在特定时间触发本地通知。(现在时间)
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.timeZone], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
关于ios - 立即使用 UNUserNotificationCenter 调用本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47341873/