如何在 iOS 中监听通知关闭事件?

How to listen for notification dismissed event in iOS?

我正在收听为我的本地通知按下的操作,但有没有办法确定用户何时关闭通知?

以下是我在 AppDelegate 中监听我的操作的方式,但关闭不会触发此消息:

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()
    }

你应该试试这个:

 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()

        }

关闭通知不会唤醒您的应用程序,因此无法捕获它。