我正在使用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)
}发布于 2019-04-08 16:34:53
当这个问题涉及到更现代的UILocalNotification时,Max的答案是反对的UNNotificationRequest。
正确的答案是传递nil。根据requestWithIdentifier:content:trigger:
trigger导致传递通知的条件。指定“零”立即传递通知。
所以在你的代码中:
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)发布于 2017-04-26 14:34:12
https://developer.apple.com/reference/uikit/uilocalnotification
如果应用程序在系统传递通知时是最重要和可见的,则调用应用程序委托的应用程序(_:didReceive:)来处理通知。使用提供的UILocalNotification对象中的信息来决定采取什么操作。该系统不显示任何警报,徽章应用程序的图标,或播放任何声音,当应用程序已经是最前面。
这是正常的行为。此外,为什么您需要通过点击按钮触发本地通知?为什么不实现所需的功能时,按钮被点击,而不是通过通知?
https://stackoverflow.com/questions/43637029
复制相似问题