Search code examples
iosobjective-cswiftpush-notificationapple-push-notifications

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.


Solution

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