Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss IOS Notification from NotificationContentExtention

Is it possible to dismiss/cancel a local notification from a button with in NotificationContentExtension?

I was only able to dismiss the NotificationContentExtension itself, but not the entire notification.

if #available(iOSApplicationExtension 12.0, *) {
          self.extensionContext?.dismissNotificationContentExtension()
}
like image 673
YardenST Avatar asked Jun 25 '20 22:06

YardenST


People also ask

What is unnotificationcontentextension?

An object that presents a custom interface for a delivered local or remote notification. The UNNotificationContentExtension protocol provides the entry point for a notification content app extension, which displays a custom interface for your app’s notifications.

How to define a notification content app extension in Xcode?

To define a notification content app extension, add a notification content extension target to the Xcode project containing your app. The default Xcode template contains a source file and storyboard for your view controller. The Info.plist file of the extension comes mostly configured.

How do I add action buttons to a notification category?

If the notification category includes custom actions, the system automatically adds action buttons to your notification interface; don't create those buttons yourself. If your view controller implements the optional didReceive (_:completionHandler:) method, the system calls that method to respond to any selected actions.

How can I improve the way my notifications are displayed?

You create this type of extension to improve the way your notifications are presented, possibly by adding custom colors and branding, or by incorporating media and other dynamic content into your notification interface.


1 Answers

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: "ClearNotif", title: "Clear", options: [])
let category = UNNotificationCategory(identifier: "ClearNotifCategory", 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 == "ClearNotif" {
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().removeAllDeliveredNotifications()
    }
    completion(.dismiss)
}

Try it and let me know it works.

like image 147
Shashank Mishra Avatar answered Sep 30 '22 14:09

Shashank Mishra