[英]How can I make UNNotificationRequest pop immediately?
I'm working with UNNotificationRequest
and I want that the notification popup immediately when I click on the button. 我正在使用
UNNotificationRequest
,我希望在单击按钮时立即弹出通知。 but in this case it appears when I quit the application. 但在这种情况下,它会在我退出应用程序时出现。 Here's my code
这是我的代码
@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)
}
Max's answer is for the deprecated UILocalNotification
when the question is regarding the more modern UNNotificationRequest
. Max的回答是已过时的
UILocalNotification
当问题是关于更现代UNNotificationRequest
。
The correct answer is to pass nil
along. 正确的答案是通过
nil
。 According to the documentation for UNNotificationRequest
's requestWithIdentifier:content:trigger:
根据
UNNotificationRequest
的requestWithIdentifier:content:trigger:
trigger
The condition that causes the notification to be delivered.
导致通知传递的条件。 Specify nil to deliver the notification right away.
指定nil立即发送通知。
So in your code: 所以在你的代码中:
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
https://developer.apple.com/reference/uikit/uilocalnotification https://developer.apple.com/reference/uikit/uilocalnotification
If the app is foremost and visible when the system delivers the notification, the app delegate's application(_:didReceive:) is called to process the notification.
如果应用程序在系统发送通知时最重要且可见,则会调用应用程序委托的应用程序(_:didReceive :)来处理通知。 Use the information in the provided UILocalNotification object to decide what action to take.
使用提供的UILocalNotification对象中的信息来确定要采取的操作。 The system does not display any alerts, badge the app's icon, or play any sounds when the app is already frontmost.
当应用程序已经位于最前端时,系统不会显示任何警报,标记应用程序的图标或播放任何声音。
That's the normal behavior. 这是正常的行为。 Besides why do you need to trigger a local notification by tapping a button?
此外,为什么你需要通过点击按钮触发本地通知? Why not to implement the desired functionality when the button is tapped and not trough notification?
为什么不在点击按钮而不通知通知时实现所需的功能?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.