我发送7条消息抛出GCM并收到35条消息的响应

时间:2016-07-24 15:14:47

标签: java http google-cloud-messaging multicast

我在http上使用Gcm多播消息,我提供任意数量的ID,并发送消息...... 我的第一个多播包含35个ID(35个目标用户)... 第二个和第三个并继续多播只包含7个ID但我仍然收到35个消息的响应(而不是7个)

  

通过http发送的代码:

  try{
HttpURLConnection connection = (HttpURLConnection) new         URL("https://android.googleapis.com/gcm/send").openConnection();

 connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Authorization", "key=AIzaSyASH05wdoY42OkmCvGwEbkihKzcwRCfDnQ");

// Write to the connection
  OutputStream output = connection.getOutputStream();
output.write(content.getBytes(charset));// content is a Json message with 7 IDs in the field (registration_ids:"xxxxx","xxxx",....)
output.close();

InputStream inputStream = connection.getErrorStream();
if (inputStream == null)
    inputStream = connection.getInputStream();

// Read everything from our stream
BufferedReader responseReader = new BufferedReader(new       InputStreamReader(inputStream, charset));

String inputLine;


while ((inputLine = responseReader.readLine()) != null) {
    response.append(inputLine);
}
responseReader.close();
}catch(IOException io)
{
System.err.println("Error3 "+io);
}
return response.toString();
}
   try{
HttpURLConnection connection = (HttpURLConnection) new URL(http_url_old).openConnection();

 connection.setDoOutput(true);
 connection.setRequestProperty("Content-type", "application/json");
 connection.setRequestProperty("Authorization",     "key=AIzaSyASH05wdoY42OkmCvGwEbkihKzcwRCfDnQ");

// Write to the connection

OutputStream output = connection.getOutputStream();
output.write(content.getBytes(charset));
output.close();

// Check the error stream first, if this is null then there have been no issues with the request
InputStream inputStream = connection.getErrorStream();
if (inputStream == null)
    inputStream = connection.getInputStream();

// Read everything from our stream
BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream, charset));

String inputLine;


while ((inputLine = responseReader.readLine()) != null) {
    response.append(inputLine);
}
responseReader.close();
}catch(IOException io)
{
 System.err.println("Error3 "+io);
}
 return response.toString();
} 

但是当我收到A响应时,服务器响应如下:

  

{" multicast_id":6414471465709567733,"成功":28,"失效":7," canonical_ids":0,&# 34;结果":[{"消息_.... ECT

28 + 7 = 35,我只发送7条消息! 在我多次发送之后响应(35)只包含7个Ids的多播!

如何得到正确的回应?

1 个答案:

答案 0 :(得分:1)

似乎是错误的错误:

response.append(inputLine);

我追加新结果,以便“响应”字符串不断增长

解决方案:每次发送新的多播时重置String ..