简体   繁体   中英

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

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 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. The remote message gives you a Map<String, String> and their is no matching constructor for creating a json object.

Add gson to your 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:

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.

Faced this issue when migrating from GCM to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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