![](/img/trans.png)
[英]How can I get UNNotificationRequest array using for loop to populate SwiftUI List view?
[英]How can I make UNNotificationRequest pop immediately?
我正在使用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)
}
Max的回答是已过时的UILocalNotification
当问题是关于更现代UNNotificationRequest
。
正确的答案是通过nil
。 根据UNNotificationRequest
的requestWithIdentifier:content:trigger:
trigger
导致通知传递的条件。 指定nil立即发送通知。
所以在你的代码中:
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
https://developer.apple.com/reference/uikit/uilocalnotification
如果应用程序在系统发送通知时最重要且可见,则会调用应用程序委托的应用程序(_:didReceive :)来处理通知。 使用提供的UILocalNotification对象中的信息来确定要采取的操作。 当应用程序已经位于最前端时,系统不会显示任何警报,标记应用程序的图标或播放任何声音。
这是正常的行为。 此外,为什么你需要通过点击按钮触发本地通知? 为什么不在点击按钮而不通知通知时实现所需的功能?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.