How to detect “clear” notifications

后端 未结 3 411
深忆病人
深忆病人 2020-12-18 05:05

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 no

相关标签:
3条回答
  • 2020-12-18 05:46

    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()
    }
    
    0 讨论(0)
  • 2020-12-18 05:49

    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
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-18 05:50

    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

    0 讨论(0)
提交回复
热议问题