我正在使用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)
}

最佳答案

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

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



所以在你的代码中:

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

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

10-11 17:10