简体   繁体   English

如何编写Google Cloud Messaging的POST请求?

[英]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. 我正在使用Google Cloud Messaging将消息发送给客户端,但我想将这些消息发送给多个收件人,而不是一个。 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". 因此,我尝试将“ to”替换为“ registration_ids”,然后提供ID列表,但收到响应,提示“ registration_ids字段不是JSONArray”。

Here is the Java code I wrote: 这是我编写的Java代码:

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? 提供收件人列表的正确方法是什么?

You dont need [ and ] to surround your jsonArray . 您不需要[]包围您的jsonArray You should change your first 7 lines of code to the following: 您应该将前7行代码更改为以下内容:

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 : 另外,用JSONObjectJSONArray构造json负载也更好:

        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();

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

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