如何在didReceiveRemoteNotification中获取userInfo JSON值 [英] How to get userInfo JSON Value inside didReceiveRemoteNotification

查看:145
本文介绍了如何在didReceiveRemoteNotification中获取userInfo JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的userInfo ["test"] JSON值为:

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 : 

我可以获取我的userInfo数据,但是我不知道为什么我的味精什么都不值(甚至没有nil也没什么要显示的) 我怎样才能直到print(msg)?

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

已更新

这就是我的推力:

$ this-> parsepush-> send(array("channels" => ['test'],"data" =>> $ post_data));

$this->parsepush->send( array( "channels" => ['test'], "data" => > $post_data ));

内部$ post_data示例:

inside $post_data example :

$ post_data = json_encode(数组 ('link'=> $ link,'message'=> $ message)); \

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

我已经尝试过使用指令,但是我也无法进行简单的打印.

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

从userInfo获取值

编辑

仍然跳过了"aps"部分.

Still skipped the "aps" part.

图片1

我的JSON

这是我的Json外观:

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

这是我的代码:

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

但是我仍然无法获取我的aps数据.仍然为零或一无所有.

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

如果需要特定的代码,只需单击下面的链接:

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

完整代码AppDelegate.swift

编辑2

我直接使用print(userInfo),打印输出为:

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

对象已保存. [消息:iOS推送,APS:{},链接:]

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

更新2

我尝试从parse.com而非我的网站(第三方)推送. 这就是我从普通print(userInfo)中得到的

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;
}]

所以我想我将进行更改,并将一些内容添加到我的JSON中,从我的网站添加到:

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;
}]

类似的东西吗?

推荐答案

如注释中所述,APNS有效负载中没有密钥test.
如果保证总是发送密钥message的值,则可以轻松解开该值

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)

否则使用可选绑定

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

要从aps键获取alert词典并打印例如body字符串,请使用

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"]!)
}

这篇关于如何在didReceiveRemoteNotification中获取userInfo JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆