gpt4 book ai didi

ios - 我怎样才能让 UNNotificationRequest 立即弹出?

转载 作者:搜寻专家 更新时间:2023-10-31 22:57:24 25 4
gpt4 key购买 nike

我正在使用 UNNotificationRequest,我希望在单击按钮时立即弹出通知。但在这种情况下,它会在我退出应用程序时出现。这是我的代码

@IBAction func shortNotifBtn(_ sender: Any) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Late wake up call"
content.body = "The early bird catches the worm, but the second mouse gets the cheese."
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
}

最佳答案

当问题是关于更现代的 UNNotificationRequest 时,Max 的回答是针对已弃用的 UILocalNotification

正确答案是传递 nil。根据documentation for UNNotificationRequest's requestWithIdentifier:content:trigger:

trigger

The condition that causes the notification to be delivered. Specify nil to deliver the notification right away.

所以在你的代码中:

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)

关于ios - 我怎样才能让 UNNotificationRequest 立即弹出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com