Sending message to gcm topic
<p>I'm trying to send a message to some topic, but for some reason the message take a lot of time to be delivered and some times I receive too many messages at once, I also noticed that some of the new messages arrive before other older messages. </p>

<pre><code>pubSub.subscribe(token, "/topics/" + topic, null);
</code></pre>

<p>The code to send messages: </p>

<pre><code>JSONObject jsonGcmData = new JSONObject();
JSONObject jsonData = new JSONObject();
jsonData.put("message", message);

if (topic != null)
{
    jsonGcmData.put("to", "/topics/" + topic);
}
else
{
    // TODO: handle this
    jsonGcmData.put("to", "/topics/Fail");
}

jsonGcmData.put("data", jsonData);

URL url = new URL("https://android.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + API_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);

OutputStream outputStream = conn.getOutputStream();
outputStream.write(jsonGcmData.toString().getBytes());

InputStream inputStream = conn.getInputStream();
</code></pre>