Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 358
Steven tan Avatar asked Dec 21 '15 16:12

Steven tan


2 Answers

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 aps = userInfo["aps"] as? [String:Any],
   let alertDict = aps["alert"] as? [String:String] {
    print("body :", alertDict["body"]!)
}
like image 96
vadian Avatar answered Nov 02 '22 05:11

vadian


I use APNs Provider and json payload as below

{
  "aps" : {
    "alert" : {
      "title" : "I am title",
      "body" : "message body."
    },
  "sound" : "default",
  "badge" : 1
  }
}

Due to the provider originates it as a JSON-defined dictionary that iOS converts to an NSDictionary object, without subscript like Dictionary, but can use value(forKey:)

Reference from here

This's my way for Swift 4

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    guard application.applicationState == .active else { return }
    guard let alertDict = ((userInfo["aps"] as? NSDictionary)?.value(forKey: "alert")) as? NSDictionary,
        let title = alertDict["title"] as? String,
        let body = alertDict["body"] as? String
        else { return }
    let alertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
    let okAct = UIAlertAction(title: "Ok", style: .default, handler: nil)
    alertController.addAction(okAct)
    self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
    completionHandler(UIBackgroundFetchResult.noData)
}
like image 22
Joshpy Avatar answered Nov 02 '22 04:11

Joshpy