有没有办法在 Swift 中立即发送本地通知?

Is there a way to deliver a local notification instantly in Swift?

我有一个函数,想在执行该函数时发送本地通知。现在我一直在使用时间间隔通知触发器并将间隔设置为 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)