How to detect "clear" notifications
Asked Answered
M

3

9

if more than a minute pass from the time a user notification arrived to notification center, there is a "clear" option to dismiss one or more notifications at once from notification center.

How the iOS OS notify that the user tapped on "clear" to dismiss several notifications together?

Mathura answered 10/1, 2018 at 16:14 Comment(5)
It does not, iOS only allows your app to interacte with a selected notification.Lethe
Yep iOS doesn't tell your app if a notification is cleared as that's the whole point. The user says they don't want to do anything with it.Sigismund
But I need to analyse that response (same as if the user delete a single notification)Mathura
Well you might need to, but you are not able to! Apple does not provide any API for what you want so you're out of luck.Lethe
I'm curious about the if more than a minute condition. Was this in some Apple docs? Or did you figure it out by trial and error? Thanks!Coeliac
O
11

Van's anwser goes straight into the right direction, but we do not need to implement the custom action to get what the question giver wanted.

If you create the category and pass it to the UNUserNotificationCenter you get a callback on the delegates didReceive function even if the user tabbed on the builtin Clear Button or the "X" Button on the content extension. The ResponeIdentifier will then be response.actionIdentifier == UNNotificationDismissActionIdentifier.

The Category must be something like that:

//Create the category...
UNNotificationCategory(identifier: "YourCustomIdentifier",
actions: [], intentIdentifiers: [], options: .customDismissAction)

//... and pass it to the UNUserNotificationCenter
UNUserNotificationCenter.current().setNotificationCategories(notificationCategories)

The category triggers the magic in the iOS framework and suddenly you get callbacks in your delegate. The delegate function should look like:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                        didReceive response: UNNotificationResponse,
                        withCompletionHandler completionHandler: @escaping () -> Void) {
  if response.actionIdentifier == UNNotificationDismissActionIdentifier {
    // notification has been dismissed somehow        
  }
  completionHandler()
}
Onetoone answered 9/7, 2019 at 12:26 Comment(6)
This options: .customDismissAction is key to getting the delegate fire the UNNotificationDismissActionIdentifier. Thanks!Chromatism
What should we use for YourCustomIdentifier?Beckon
You must set the same identifier for that category that you have set in the payload of the notification messages you want to dismiss. Without an identifier matching the one of the category with the custom action, the system will not trigger the custom dismiss action, because the message ist not "bound" to the category.Onetoone
@Onetoone userNotificationCenter gets called if you clear a single notification, but if you clear all of them at once using the x/clear button then this method isn't called. At least on my side is not working.Involved
This misses the point of the original question.. this does not detect "clear all" action. Just when the users clears a single notification.Twitch
Thanks, @Onetoone I was looking for this for so long.Prelect
W
0

its possible from iOS 10 and above with implementation of custom notification, you will need to work with UNNotificaitons

private func registerForRemoteNotificaiton(_ application: UIApplication) {
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    if #available(iOS 10.0, *) {
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {(granted, error) in
                if granted {
                    DispatchQueue.main.async(execute: {
                        UIApplication.shared.registerForRemoteNotifications()
                    })
                }
        })
        configureUserNotification()
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        // For iOS 10 data message (sent via FCM)
        Messaging.messaging().delegate = self as MessagingDelegate
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    // [END register_for_notifications]
}

private func configureUserNotification() {
    if #available(iOS 10.0, *) {
        let action = UNNotificationAction(identifier: UNNotificationDismissActionIdentifier, title: "Cancel", options: [])
        //let action1 = UNNotificationAction(identifier: "dismiss", title: "OK", options: [])
        let category = UNNotificationCategory(identifier: "myNotificationCategory", actions: [action], intentIdentifiers: [], options: .customDismissAction)
        UNUserNotificationCenter.current().setNotificationCategories([category])
    } else {
        // Fallback on earlier versions
    }

}

call registerForRemoteNotificaiton method from appdelegate's didFinishLaunching method.

Then you will need to implement UNUserNotificationCenterDelegate in appdelegate.

and then you will get the clear (here "Dismiss" as we added in action name)

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.actionIdentifier == UNNotificationDismissActionIdentifier {
        //user dismissed the notifcaiton:

    }

    completionHandler()
}

find more information here

Whitelivered answered 11/1, 2018 at 12:53 Comment(1)
this is not clear action but a custom one you added called 'dismiss'.Fragrance
D
0

First you should set the UNUserNotificationCenterDelegate:

import UserNotifications

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    return true
}

then user the delegate functinos

extension AppDelegate: UNUserNotificationCenterDelegate {
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    switch response.actionIdentifier {
    case UNNotificationDismissActionIdentifier:
      // Do whatever you want to do on dismiss action, when user clear the notification
      break

    case UNNotificationDefaultActionIdentifier:
            // Do whatever you want to do on tap action, when user tap on notification and your application will open
      break

    default:
      break
    }
  }
}
Dorn answered 19/3, 2020 at 3:36 Comment(1)
With this exact code, tapping clear button does not call didReceive method.Beckon

© 2022 - 2024 — McMap. All rights reserved.