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

标签 swift iphone notifications ios12

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

相关文章:

ios - 连接前识别CBPeripheral的类型

Swift Playground - 如何将带逗号的字符串转换为带小数的字符串

javascript - 将Notification.request Permission() 对话框替换为html 对话框

ios - SKView!提交时发现为零

swift - 您只能将计算变量与 setter 和 getter 一起使用来覆盖父类(super class)的存储属性

iphone - iOS - 如何正确访问数组中的对象属性?

iphone - 按钮文本未对齐 Safari

ios - 自动布局 : Position a table on top of an image using proportional spacing

android - 检索 StatusbarNotification 详细信息(标题、通知文本)的可靠方法

amazon-web-services - AWS SNS : Case of multiple subscribers