How to listen for notification dismissed event in

2019-04-19 00:10发布

问题:

I am listneing to actions pressed for my local notifications, but is there a way to determine when the user dismisses a notification?

Here is how I'm listening to my actions in my AppDelegate, but the dismiss doesn't fire this:

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
        var actionName: String? = nil

        if let identifier = identifier {
            switch identifier {
                case "snoozeAction":
                    actionName = "snoozeActionTapped"
                    break
                default: break
            }

            if let name = actionName {
                NSNotificationCenter.defaultCenter().postNotificationName(name, object: nil)
            }
        }

        completionHandler()
    }

回答1:

Dismissing a notification does not wake your application up so there is no way to capture this.



回答2:

You should try this:

 func application(application: UIApplication!,
            handleActionWithIdentifier identifier:String!,
            forLocalNotification notification:UILocalNotification!,
            completionHandler: (() -> Void)!){

                if (identifier == "FIRST_ACTION"){

                    NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil)

                }else if (identifier == "SECOND_ACTION"){
                    NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil)

                }

                completionHandler()

        }