jp1987
jp1987

Reputation: 141

UNNotificationAction doesn't dismiss after choice

I am using Firebase to send remote messages, like so:

AppDelegate (pseudo code):

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()
}

The notifications are sent when the app is in the background (only when the Phone app is active, after picking up a call).

Receiving the notifications works fine, I can see both Actions and make a selection. After selection, the app comes to the foreground. However, the notification does not dismiss. I have tried using UNUserNotificationCenter.current().removeAllDeliveredNotifications() in multiple places (also in the didReceive handler), but the notification never disappears. I have found many issues on Stack Overflow with removeAllDeliveredNotifications (or getDeliveredNotifications) in recent iOS versions.

I have tried removing .foreground as an option, this does dismiss the notification (after selecting an action). But obviously does not bring my app to the foreground, which is the intended functionality.

Upvotes: 0

Views: 536

Answers (1)

manishsharma93
manishsharma93

Reputation: 1049

You need to add this method

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")
    }
}

Upvotes: 0

Related Questions