How to detect “clear” notifications

雨燕双飞 提交于 2019-11-29 11:54:58

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

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()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!