Push notification Swift 2.1 |通过操作接收远程推送通知

Push notification Swift 2.1 |通过操作接收远程推送通知,push-notification,swift2,action,Push Notification,Swift2,Action,在过去的5个小时里,我一直在想如何在swift2.1中显示远程推送通知的操作 事实: 1-我收到通知,但它没有显示选项 2-我能够生成带有操作的本地通知,没有aby问题 代码: 帮助检查是否有此帮助!检查这是否有帮助! enum Actions:String{ case increment = "INCREMENT_ACTION" case decrement = "DECREMENT_ACTION" case reset = "RESET_ACTION" }

在过去的5个小时里,我一直在想如何在swift2.1中显示远程推送通知的操作

事实: 1-我收到通知,但它没有显示选项 2-我能够生成带有操作的本地通知,没有aby问题

代码:

帮助

检查是否有此帮助!检查这是否有帮助!
    enum Actions:String{
    case increment = "INCREMENT_ACTION"
    case decrement = "DECREMENT_ACTION"
    case reset = "RESET_ACTION"
}

var categoryID:String {
    get{
        return "COUNTER_CATEGORY"
    }
}

    func registerNotification() {


    //removeNotification()


    // 1. Create the actions **************************************************

    // increment Action
    let incrementAction = UIMutableUserNotificationAction()
    incrementAction.identifier = Actions.increment.rawValue
    incrementAction.title = "Yes"
    incrementAction.activationMode = UIUserNotificationActivationMode.Background
    incrementAction.authenticationRequired = false
    incrementAction.destructive = false

    // decrement Action
    let decrementAction = UIMutableUserNotificationAction()
    decrementAction.identifier = Actions.decrement.rawValue
    decrementAction.title = "No"
    decrementAction.activationMode = UIUserNotificationActivationMode.Background
    decrementAction.authenticationRequired = true
    decrementAction.destructive = false

    // reset Action
    let resetAction = UIMutableUserNotificationAction()
    resetAction.identifier = Actions.reset.rawValue
    resetAction.title = "RESET"
    resetAction.activationMode = UIUserNotificationActivationMode.Foreground
    // NOT USED resetAction.authenticationRequired = true
    resetAction.destructive = true


    // 2. Create the category ***********************************************

    // Category
    let counterCategory = UIMutableUserNotificationCategory()
    counterCategory.identifier = categoryID

    // A. Set actions for the default context
    counterCategory.setActions([incrementAction, decrementAction, resetAction],
        forContext: UIUserNotificationActionContext.Default)

    // B. Set actions for the minimal context
    counterCategory.setActions([incrementAction, decrementAction],
        forContext: UIUserNotificationActionContext.Minimal)


    // 3. Notification Registration *****************************************

    let types = UIUserNotificationType.Alert //| UIUserNotificationType.Sound
    let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: counterCategory) as! Set<UIUserNotificationCategory>)


    print("About to register remote notification")

    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    UIApplication.sharedApplication().registerForRemoteNotifications()




}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!) {
    print("Got token data! \(deviceToken)")
    print(deviceToken.hexString)


}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    print("Recived: \(userInfo)")

    //Parsing userinfo:
    var temp : NSDictionary = userInfo
    if let info = userInfo["aps"] as? Dictionary<String, AnyObject>
    {
        var alertMsg = info["alert"] as! String
    }


}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
    print("Im here")
    if userInfo["category"] as! String  == categoryID {
        print("Yes")
        let action:Actions = Actions(rawValue: identifier!)!
        var counter = 0;

        switch action{

        case Actions.increment:
            attemptReconnectionToSocket()

        case Actions.decrement:
            counter--

        case Actions.reset:
            counter=0

        }
    }

    completionHandler()

}