简体   繁体   English

Firebase Android:如何通过 RemoteMessage 访问嵌套在数据中的参数?

[英]Firebase Android: how do I access params nested in data via RemoteMessage?

via this shape:通过这个形状:

{
  "to": "000",
  "priority": "high",
  "data": {
    "title": "A Title",
    "message": "A Message",
    "link": {
      "url": "http://www.espn.com",
      "text": "ESPN",
    }
  }
}

how can I access "url" and "text"?如何访问“url”和“text”?

String messageLink = remoteMessage.getData().get("link");

gets me:让我:

{"text":"ESPN","url":"http://www.espn.com"}

but how do I drill deeper?但我如何钻得更深?

remoteMessage.getData().get("link").get("text");

doesnt quite work... I have also attempted JSONObject:不太工作......我也尝试过JSONObject:

JSONObject json = new JSONObject(remoteMessage.getData());    
JSONObject link = json.getJSONObject("link");

but this gives me try catch errors...但这让我尝试捕捉错误...

Any help and direction as always is greatly appreciated!一如既往的任何帮助和指导,我们将不胜感激!

I would use gson and define a model class.我会使用 gson 并定义一个模型类。 The remote message gives you a Map<String, String> and their is no matching constructor for creating a json object.远程消息为您提供Map<String, String>并且它们不是用于创建 json 对象的匹配构造函数。

Add gson to your build.xml:将 gson 添加到您的 build.xml 中:

compile 'com.google.code.gson:gson:2.5'

Create a notification model:创建通知模型:

import com.google.gson.annotations.SerializedName;

public class Notification {

    @SerializedName("title")
    String title;
    @SerializedName("message")
    String message;
    @SerializedName("link")
    private Link link;

    public String getTitle() {
        return title;
    }

    public String getMessage() {
        return message;
    }

    public Link getLink() {
        return link;
    }

    public class Link {

        @SerializedName("url")
        String url;
        @SerializedName("text")
        String text;

        public String getUrl() {
            return url;
        }

        public String getText() {
            return text;
        }

    }

}

Deserialize a notification object from the remote message.从远程消息反序列化一个通知对象。

If all your custom keys are at the top level:如果您的所有自定义键都在顶级:

Notification notification = gson.fromJson(gson.toJson(remoteMessage.getData()), Notification.class);

If your custom json data is nested in a single key for example "data" then use:如果您的自定义 json 数据嵌套在单个键中,例如“数据”,则使用:

Notification notification = gson.fromJson(remoteMessage.getData().get("data"), Notification.class);

Note in this simple case the @SerializedName() annotations are unnecessary since the field names exactly match the keys in the json, but if you for example have a key name start_time but you want to name the java field startTime you would need the annotation.请注意,在这种简单的情况下, @SerializedName()注释是不必要的,因为字段名称与 json 中的键完全匹配,但是如果您例如有一个键名start_time但您想命名 java 字段startTime您将需要该注释。

Faced this issue when migrating from GCM to FCM.从 GCM 迁移到 FCM 时遇到了这个问题。

The following is working for my use case, so perhaps it will work for you.以下内容适用于我的用例,所以也许它对你有用。

JsonObject jsonObject = new JsonObject(); // com.google.gson.JsonObject
JsonParser jsonParser = new JsonParser(); // com.google.gson.JsonParser
Map<String, String> map = remoteMessage.getData();
String val;

for (String key : map.keySet()) {
    val = map.get(key);
    try {
        jsonObject.add(key, jsonParser.parse(val));
    } catch (Exception e) {
        jsonObject.addProperty(key, val);
    }
}

// Now you can traverse jsonObject, or use to populate a custom object:
// MyObj o = new Gson().fromJson(jsonObject, MyObj.class)

As simple as that:就这么简单:

String linkData = remoteMessage.getData().get("link");
JSONObject linkObject = new JSONObject(linkData);

String url = linkObject.getString("url");
String text = linkObject.getString("text");

Of course, together with proper error handling.当然,还有适当的错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM