gpt4 book ai didi

ios - 选择后 UNNotificationAction 不会关闭

转载 作者:行者123 更新时间:2023-11-30 11:17:13 28 4
gpt4 key购买 nike

我正在使用 Firebase 发送远程消息,如下所示:

AppDelegate(伪代码):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self

UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) {granted, error in
let actionWithText = UNTextInputNotificationAction(identifier: "actionWithText", title: "Action with text", options: [.foreground], textInputButtonTitle: "Send", textInputPlaceholder: "Placeholder")
let action = UNNotificationAction(identifier: "action", title: "Action without text", options: [.foreground])
let category = UNNotificationCategory(identifier: "category", actions: [actionWithText, action], intentIdentifiers: [], options: [])

UNUserNotificationCenter.current().setNotificationCategories([category])
}

application.registerForRemoteNotifications()

return true
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "actionWithText" {
if let textResponse = response as? UNTextInputNotificationResponse {
let textResponse = textResponse.userText
}
}

if response.actionIdentifier == "action" {
print("action received")
}

completionHandler()
}

当应用程序在后台运行时(仅当电话应用程序处于事件状态、接听电话后),才会发送通知。

接收通知工作正常,我可以看到两个操作并做出选择。选择后,应用程序将进入前台。然而,该通知并没有驳回。我尝试在多个地方(也在 didReceive 处理程序中)使用 UNUserNotificationCenter.current().removeAllDeliveredNotifications(),但通知永远不会消失。我在最近的 iOS 版本中使用 removeAllDeliveredNotifications(或 getDeliveredNotifications)在 Stack Overflow 上发现了许多问题。

我尝试删除 .foreground 作为一个选项,这确实会消除通知(选择操作后)。但显然不会将我的应用程序带到前台,这是预期的功能。

最佳答案

您需要添加此方法

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {

if response.actionIdentifier == "Clear" {
completion(.dismiss)
print("Clear. It will dismiss the notification")
}else if (response.actionIdentifier == "Next"){
completion(.doNotDismiss)
print("Next. It will not dismiss the notification")
}
}

关于ios - 选择后 UNNotificationAction 不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633017/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com