我正在使用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
作为选项,但这确实消除了该通知(选择一个动作之后)。但显然并没有使我的应用程序成为预期的功能。
答案 0 :(得分:0)
您需要添加此方法
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")
}
}