简体   繁体   English

如何立即弹出UNNotificationRequest?

[英]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: 根据UNNotificationRequestrequestWithIdentifier: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.

相关问题 如何使用 for 循环获取 UNNotificationRequest 数组以填充 SwiftUI 列表视图? - How can I get UNNotificationRequest array using for loop to populate SwiftUI List view? 如何使UIViewController弹出Swift SWRevealViewController? - How can I make UIViewController pop Swift SWRevealViewController? 如何在导航弹出窗口上调整UIPopoverPresentationController的大小? - How can I make UIPopoverPresentationController resize on navigation pop? 如何在Swift中的while循环的每次迭代中立即发生语音? - How can I make speech occur immediately for each iteration of a while loop in Swift? 如何使用IOS(Swift)和laravel后端制作Messenger应用? 消息不立即 - How can I make a Messenger App with IOS (Swift) and laravel backend? message not immediately 当应用程序进入后台时,如何使我的应用程序弹出到VC? - How can I make my app pop to a VC when the app enters the background? 如何在PinView中使UIButton打开弹出视图? - How can I make a UIButton within PinView open a pop up view? 如何从 web 应用程序在 iPhone 上弹出联系人? - How can I make a contact pop up on an iPhone from a web application? 是否可以有多个未完成的UNNotificationRequest? - Can there be more than one outstanding UNNotificationRequest? 如何在按下按钮时播放音频,而当我将手指从同一按钮上移开时如何立即停止播放? - How can I make audio play when the button is pressed and immediately stop when I take my finger off the same button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM