Shourob Datta
Shourob Datta

Reputation: 2092

How to remove UNNotificationContentExtension displayed view

I used UNNotificationContentExtension to survey from the user.

enter image description here

Condition is I do not open the parent app.

Here is the emoji action

    if #available(iOSApplicationExtension 12.0, *) {

        // API call here
        self.extensionContext?.dismissNotificationContentExtension()
    } else {
        // Fallback on earlier versions
    }

Each emoji have actions. When user tap the emoji I will send the response into server and remove this notification. Everythings will happens on the extension part

What's the issue?

Using dismissNotificationContentExtension notification dismiss and hide instant. Its again found in the notification screen. How could I remove this notification when user tap emoji button.

Upvotes: 1

Views: 936

Answers (3)

Marián Černý
Marián Černý

Reputation: 15778

You can remove the current notification using removeDeliveredNotifications(withIdentifiers:).

var notification: UNNotification?

func didReceive(_ notification: UNNotification) {
    self.notification = notification

    ...
}

@IBAction func btnActionHappy(_ sender: Any) {
    if #available(iOSApplicationExtension 12.0, *) {
        extensionContext?.dismissNotificationContentExtension()
    }

    if let identifier = notification?.request.identifier {
        let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: [identifier])
    }
}

Upvotes: 0

Shourob Datta
Shourob Datta

Reputation: 2092

This is how my solution working. Cons: All delivered notification of the same category removed instead of doing remove current message.

@IBAction func btnActionHappy(_ sender: Any) {
    
       UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
            if #available(iOSApplicationExtension 12.0, *) {
                self.extensionContext?.dismissNotificationContentExtension()
            } else {
                // Fallback on earlier versions
            }

            let matchingNotifications = notifications.filter({ $0.request.content.categoryIdentifier == "debitOverdraftNotification" })
            UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: matchingNotifications.map({ $0.request.identifier }))
            
            print("Somethings")

        }
}

Upvotes: 2

Shashank Mishra
Shashank Mishra

Reputation: 1077

You can do it using UNUserNotificationCenter & UNNotificationContentExtension protocol

Add action using UNUserNotificationCenter

let center = UNUserNotificationCenter.current()
center.delegate = self  
center.requestAuthorization (options: [.alert, .sound]) {(_, _) in 
}  
let clearAction = UNNotificationAction(identifier: "sadEmoji", title: "Emoji", options: [])
let category = UNNotificationCategory(identifier: "NotifCategory", actions: [clearAction], intentIdentifiers: [], options: [])
 center.setNotificationCategories([category])

Add a delegate method of the protocol UNNotificationContentExtension in your extension's view controller

 func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
    if response.actionIdentifier == "sadEmoji" {
        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: "NotifCategory")
    }
    completion(.dismiss)
}

Try it and let me know it works.

Upvotes: 1

Related Questions