Kritias
Kritias

Reputation: 187

How to write the POST request for Google Cloud Messaging?

I am using Google Cloud Messaging to send messages to clients but I want to send those to multiple recipients instead of one. Here is the simple body of the request I am currently sending:

https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=API_KEY
{
"to" : "REGISTRATION_TOKEN",
"notification" : {
 "sound" : "default",
 "badge" : "1",
 "title" : "default",
 "body"  : "Test"
 },
"content_available" : true
}

So I try to replace "to" by "registration_ids" and then provide the list of ids but I get a response saying "registration_ids field is not a JSONArray".

Here is the Java code I wrote:

public static void sendGCM(String tag, String message, ArrayList<String> listeDeviceIds) {
    String registrationIds = listeDeviceIds.get(0);
    for (int i=1; i<listeDeviceIds.size(); i++){
        registrationIds = registrationIds+","+listeDeviceIds.get(i);
    }
    JSONArray jsonArray = new JSONArray();
    jsonArray.addAll(listeDeviceIds);
    String json ="{\"registration_ids\" : \"["+jsonArray+"]\",\"notification\" : {\"sound\" : \"default\",\"badge\" : \"1\",\"title\" : \"default\",\"body\"  : \"Test\"},\"content_available\" : true}"; 
    try{
        URL url = new URL("https://android.googleapis.com/gcm/send");
        HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
        request.addHeader(new HTTPHeader("Content-Type","application/json")); 
        request.addHeader(new HTTPHeader("Authorization", "key="+API_KEY));
        request.setPayload(json.getBytes("UTF-8"));
        HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
    }
    catch(Exception e){
        e.printStackTrace();
    }

}

What is the proper way to provide a list of recipients?

Upvotes: 0

Views: 1292

Answers (1)

ztan
ztan

Reputation: 6931

You dont need [ and ] to surround your jsonArray. You should change your first 7 lines of code to the following:

JSONArray jsonArray = new JSONArray();
for (String id : listeDeviceIds) {
   jsonArray.put(id);
}
String json ="{\"registration_ids\" : "+jsonArray+",\"notification\" : {\"sound\" : \"default\",\"badge\" : \"1\",\"title\" : \"default\",\"body\"  : \"Test\"},\"content_available\" : true}";

Alternatively, its also better to construct your json payload with JSONObject and JSONArray:

        String json = "";

        JSONObject jsonObject = new JSONObject();

        JSONArray regIdArray = new JSONArray();
        for (String id : listeDeviceIds) {
            regIdArray.put(id);
        }
        jsonObject.accumulate("registration_ids", regIdArray);

        JSONObject notificationObject = new JSONObject();
        notificationObject.accumulate("sound", "default");
        notificationObject.accumulate("badge", "1");
        notificationObject.accumulate("title", "default");
        notificationObject.accumulate("body", "test");
        jsonObject.accumulate("notification", notificationObject);

        jsonObject.accumulate("content_available", true);

        json = jsonObject.toString();

Upvotes: 1

Related Questions