ios - 如何在 didReceiveRemoteNotification 中获取 userInfo JSON 值

标签 ios json swift swift2

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 值是:

> 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 数据,但我不知道为什么我的 msg 没有任何值(甚至没有 nil,只是没有显示任何内容) 我怎样才能继续打印(msg)?

已更新

这是我的推送:

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

$post_data 示例中:

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

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

get Value from userInfo

编辑

仍然跳过了“aps”部分。

Picture 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 数据。仍然为零或什么都没有。

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

Full code AppDelegate.swift

编辑2

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

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

更新2

我尝试从 parse.com 推送,而不是我的网站(第 3 方)。 这就是我从正常打印(userInfo)中得到的内容

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

所以我想我只需更改并添加一些内容到我的 JSON 从我的网站到:

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

类似的事情?

最佳答案

正如评论中提到的,APNS 负载中没有关键的 test
如果保证始终发送 key 消息的值,您可以轻松解开该值

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

否则使用可选绑定(bind)

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

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

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

关于ios - 如何在 didReceiveRemoteNotification 中获取 userInfo JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34411083/

相关文章:

python - 使用 Python 在 JSON 中查找字符串

c# - 将JSON转换为C#对象格式的在线工具

ios - 解析转换为 MLab 和 Beanstalk 以及 Swift AppDelegate

ios - 快速捕获无效用户输入的异常

Swift - 从 Nib 实例化总是返回 UIViewController 类型而不是动态类型

ios - MKPolyline -> NSKeyedArchiver -> NSData SIGABRT

ios - 在移动设备上运行的 Phonegap/Cordova

java - 从 JSON 中的变量类型进行 Jackson 反序列化

ios - 滚动条滚动但 UIScrollView 不滚动

iphone - 是否可以让工厂类负责创建对象集合而不是一次创建一个对象