I am trying to implement HTTP server for GCM communication. After providing the correct keys , I get HTTP response code as '400'. Here is the code snippet:
URL url = new URL("https://android.googleapis.com/gcm/send");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.sgp.com", 8080));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key="+"AIzaSyAYc53L6kg_XerwoWdLjUAi2iEfNKidSF8");
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeUTF("{\"collapse_key\" : \"Food-Promo\", \"data\" : {\"Category\" : \"FOOD\",\"Type\": \"VEG\",}, \"registration_ids\": [\"APA91bEk7GPFVxzOidvB3JKCMWq3FHpAaTj2dBv9VGOQkKtLAEiVGR8TDi1fsU4D1k293ODAFTJ8dNfE2gzJNfCvB1sjewZu2fGOIJmY8dgjcNTZQYi4QfyQH-AaO0qEmQnbEeEtsUQ5LzWrIHboAhJMx1bfdsO9bg\"]}");
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
Can anybody help?
400 error only comes in following case:
Only applies for JSON requests.
Indicates that the request could not be parsed as JSON
, or it contained invalid fields (for instance, passing a string where a number was expected). The exact failure reason is described in the response and the problem should be addressed before the request can be retried. ReferenceSo you can try this code, its working for me
Create one POJO class for standard message format for GCM
Code to request GCM to send notification message to android device,
Hope this helps you with your problem...!!!!!