我有一个函数,想在该函数执行时发送一个本地通知。现在我一直在使用时间间隔通知触发器并将间隔设置为 0.1 秒,但这感觉并不是立即传递通知的正确方法,因为它仍然有 0.1 秒的延迟,即使它几乎不会被注意到.
我的问题是有没有一种方法可以立即发送通知,就像没有任何延迟一样?我一直在寻找解决这个问题的方法,但没有找到任何方法,希望大家能提供帮助。谢谢。
func didUpdateSettings() {
//other code...
let content = UNMutableNotificationContent()
content.title = "Notification title"
content.subtitle = "Notification subtitle"
content.body = "Notification body"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
最佳答案
根据文档:
https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649633-init
trigger The condition that causes the notification to be delivered. Specify nil to deliver the notification right away.
所以代码是:
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: nil)
关于swift - 有没有办法在 Swift 中立即发送本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57134593/