ios – “Clear All” action doesn’t trigger userNotificationCenter(.. didReceive response.. ) delegate method


I want to perform some calculations when the user dismisses a notification.
This works as expected when dismissing a single notification. But I can’t get it to work when dismissing a stack of notifications with “Clear All”.


For this purpose I created an UNNotificationCategory with option .customDismissAction,

extension UNNotificationCategory {
    static let CUSTOM_CATEGORY_IDENTIFIER: String = "CUSTOM_CATEGORY_IDENTIFIER"
    static let customCategory: UNNotificationCategory =
        .init(identifier: CUSTOM_CATEGORY_IDENTIFIER, actions: [], intentIdentifiers: [],
              options: .customDismissAction
        )
}

registered my category with UNNotificationCenter.current().setNotificationCategories()

class AppDelegate: NSObject, UNUserNotificationCenterDelegate {
    
    public func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) {
        // ...
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current()
            .setNotificationCategories([UNNotificationCategory.customCategory])
        // ...
    }
}

and for each new UNNotificationRequest I set the content.categoryIdentifier accordingly.

    func notificationRequest(title: String, body: String) -> UNNotificationRequest {
        let identifier = UUID().uuidString
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
        
        let content = UNMutableNotificationContent()
        content.title = title
        content.body = body
        content.categoryIdentifier = UNNotificationCategory.CUSTOM_CATEGORY_IDENTIFIER
        
        return UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
    }

On dismissing a single notification everything works as expected.
The userNotificationCenter(_: UNNotificationCenter, didReceive response: ..) delegate method is called, the response.actionIdentifier is UNNotificationDismissActionIdentifier as expected and my code is performed.

    public func userNotificationCenter(_: UNUserNotificationCenter,
                                       didReceive response: UNNotificationResponse,
                                       withCompletionHandler completionHandler: @escaping () -> Void) {
        switch response.actionIdentifier {
        case UNNotificationDismissActionIdentifier:
            print("handle dismiss notification")
            
        case UNNotificationDefaultActionIdentifier:
            print("handle dismiss notification")
            
        case let identifier:
            print("didReceive notificationResponse with identifier: \(identifier)")
        }
        completionHandler()
    }

But when multiple stacked notifications are dismissed all at once via swipe, “Clear All” action or “X” button, the delegate isn’t called even once.

enter image description here

Is there any way I can be notified in this case?
(doesn’t matter if dismissing a stack of x notifications would trigger the delegate once or x times)

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img