【问题标题】:How to write the POST request for Google Cloud Messaging?如何编写 Google Cloud Messaging 的 POST 请求?
【发布时间】:2015-08-23 11:52:36
【问题描述】:

我正在使用 Google Cloud Messaging 向客户发送消息,但我想将这些消息发送给多个收件人而不是一个收件人。 这是我当前发送的请求的简单正文:

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
}

所以我尝试将“to”替换为“registration_ids”,然后提供 id 列表,但我收到回复说“registration_ids 字段不是 JSONArray”。

这是我写的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();
    }

}

提供收件人列表的正确方法是什么?

【问题讨论】:

  • 你的代码是否正常工作,如果没有,那么也发布你得到的错误消息??
  • 我的代码在只有一个收件人时有效,我将“registration_ids”字段替换为“to”字段。我得到的错误是“registration_ids 字段不是 JSONArray”的东西。
  • 请发布整个错误代码..

标签: java json google-cloud-messaging


【解决方案1】:

你不需要[] 来包围你的jsonArray。您应该将前 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}";

另外,最好使用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();

【讨论】:

    猜你喜欢
    • 2018-11-02
    • 2012-07-26
    • 2020-02-23
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多