1

I have spent the last 5 hours trying to figure out how to show actions in swift2.1 for a REMOTE push notification.

Facts: 1- I am receiving the notification but it isnt showing the options 2- I was able to generate a local notification with action without aby problems

Code:

    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

// var alert: UIAlertView! // alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK") // alert.show()

    }


}

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

}

HELP!!!

user3748242
  • 73
  • 1
  • 5
  • http://stackoverflow.com/questions/31610896/controlling-which-view-controller-loads-after-receiving-a-push-notification-in-s Check if this help! – Pak Dec 17 '15 at 21:32

0 Answers0