How to get userInfo JSON Value inside didReceiveRemoteNotification

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

        if application.applicationState == UIApplicationState.Active
        {
            PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)

            if let msg = userInfo["test"] as? Dictionary<String,AnyObject>
            {
                print(msg)
            }
        }

my userInfo[“test”] JSON value is :

> Printing description of userInfo:
▿ 3 elements
  ▿ [0] : 2 elements
    - .0 : message
    - .1 : 1235
  ▿ [1] : 2 elements
    - .0 : aps
    - .1 : 0 elements
  ▿ [2] : 2 elements
    - .0 : link
    - .1 : 

I can get my userInfo data, but i don’t know why my msg value nothing ( not even nil just nothing to show)
How can i go till print(msg) ?

  • Getting yesterdays steps from HealthKit
  • Generic controller in swift 2.0 using storyboards
  • Allow users background music in swift 2.0
  • Swift 2: How to Load UITabBarController after Showing LoginViewController
  • Pass data with prepareForSegue
  • Using NSUserDefaults on arrays
  • UPDATED

    Here is what my push like :

    $this->parsepush->send( array( “channels” => [‘test’], “data” => > $post_data ));

    inside $post_data example :

    $post_data =
    json_encode(array
    (‘link’ => $link, ‘message’ => $message));\

    I’ve tried instruction from, yet i can’t do a simple print too.

    get Value from userInfo

    EDIT

    Still skipped the “aps” part.

    Picture 1

    My JSON

    Here is my Json looks like :

    > {  
      "aps":{  
         "alert":{  
            "title":"$title",
            "body":"$message"
         },
         "badge":"1"
      },
      "adira":{  
         "link":"$link"
      }
    }
    

    And this is my Code :

    >   func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
                  //  PFPush.handlePush(userInfo)
           // PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
            if let msg = userInfo["message"] as? String {
                print(msg)
                print("a")
            }     
            if let alertDict = userInfo["aps"]?["alert"] as? Dictionary<String, String> {
                print("title : ", alertDict["title"])
                print("body :", alertDict["body"]!)
                print("badge: ", alertDict["badge"]!)
                print("b")
            }      
            if let link = userInfo["link"] as? String {
                print(link)
                print("c")
            }
        }
    }
    

    But still i can’t get my aps data. still nil or nothing.

    If you want a specific code, just click link below :

    Full code AppDelegate.swift

    EDIT 2

    i use print(userInfo) directly and the printout is :

    Object has been saved.
    [message: iOS push, aps: { }, link: ]

    Update 2

    I tried to push from parse.com, not my website (3rd party).
    and this is what i get from normal print(userInfo)

    > [aps: {
        alert = 12345abc;
        sound = default;
    }]
    

    So i guess i’ll just changed and add something inside to my JSON From my Website to :

    [aps:{
       alert = $message;
       sound = default;
       link = $link;
    }]
    

    Something like that ?

    Solutions Collect From Internet About “How to get userInfo JSON Value inside didReceiveRemoteNotification”

    As mentioned in the comments, there is no key test in the APNS payload.
    If the value for key message is guaranteed to be sent always you can easily unwrap the value

    let msg = userInfo["message"] as! String
    print(msg)
    

    otherwise use optional binding

    if let msg = userInfo["message"] as? String {
        print(msg)
    }
    

    To get the alert dictionary from the aps key and print for example the body string use

    if let alertDict = userInfo["aps"]?["alert"] as? Dictionary<String, String> {
        print("body :", alertDict["body"]!)
    }