0

I have write this code for send notification using fcm by java server code. But it is throwing exception Server returned HTTP response code: 500 for URL: https://fcm.googleapis.com/fcm/send

public static void pushFCMNotification(String userDeviceIdKey) throws     Exception{

    String authKey = AUTH_KEY_FCM;   // You FCM AUTH key
    String FMCurl = API_URL_FCM;     

    URL url = new URL(FMCurl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization","key="+authKey);
    conn.setRequestProperty("Content-Type","application/json");

    JSONObject json = new JSONObject();
    json.put("to",userDeviceIdKey.trim());
    JSONObject info = new JSONObject();
    info.put("title", "Notificatoin Title");   // Notification title
    info.put("body", "Hello Test notification"); // Notification body
    json.put("notification", info);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(json.toString());
        wr.flush();
        wr.close();

        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + json);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();


    /*OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(json.toString());
    wr.flush();
    conn.getInputStream();*/
    }
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
Raj K Pachauri
  • 283
  • 2
  • 14
  • 1
    http://stackoverflow.com/questions/37516589/send-push-notifications-from-server-with-fcm – mayank Aug 25 '16 at 08:24
  • I have solved this, for the proper answer please refer the this link : http://stackoverflow.com/questions/38089148/send-push-notification-from-server-to-android-device-in-java/39142552#39142552 – Raj K Pachauri May 05 '17 at 12:51

1 Answers1

1

According to the oficial documentation:

Errors in the 500-599 range (such as 500 or 503) indicate that there was an internal error in the FCM connection server while trying to process the request, or that the server is temporarily unavailable (for example, because of timeouts). Sender must retry later, honoring any Retry-After header included in the response. Application servers must implement exponential back-off.

source: https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream

However, make sure that the JSON request is properly formatted. Try to update your question with the json.toString() and check that your API key is still valid using the procedure described in: Checking the validity of a server key

GeorgeLBA
  • 334
  • 1
  • 3
  • I have resolve that issues now i am receiving 200 status code. But at client side (on Android device ) it is not received. But using fcm console device received notification. – Raj K Pachauri Aug 25 '16 at 09:59
  • Ok, perfect. So, try the following: in your code, instead of `json.put("notification", info);` use `json.put("data", info);` and check if your onMessageReceived method is called when you send the notification from your own server implementation. If not, please edit your question and add the code you are using to receive the notification. – GeorgeLBA Aug 25 '16 at 10:55