【问题标题】:Is there a way to deliver a local notification instantly in Swift?有没有办法在 Swift 中立即发送本地通知?
【发布时间】:2019-11-29 18:07:12
【问题描述】:

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

【问题讨论】:

    标签: swift iphone notifications ios12


    【解决方案1】:

    根据文档:

    https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649633-init

    触发器 导致通知被传递的条件。指定 nil 以立即发送通知。

    所以代码是:

    let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多