【发布时间】: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