小编Gha*_*lif的帖子

如何让 UNNotificationRequest 立即弹出?

我正在使用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)
}
Run Code Online (Sandbox Code Playgroud)

notifications ios swift ios10 unnotificationrequest

8
推荐指数
2
解决办法
3532
查看次数

标签 统计

ios ×1

ios10 ×1

notifications ×1

swift ×1

unnotificationrequest ×1