title: Java Android FCM sending message to user Server returned HTTP response code
body: <p>I did this :</p>
  
  <pre><code>public class PushNotifictionHelper {
      public final static String AUTH_KEY_FCM = "AIzaSyD63pfTvnwhe9WVuIe.........";
      public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
  
      public static String sendPushNotification(String deviceToken)
              throws IOException, JSONException {
          String result = "";
          URL url = new URL(API_URL_FCM);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  
          conn.setUseCaches(false);
          conn.setDoInput(true);
          conn.setDoOutput(true);
  
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM);
          conn.setRequestProperty("Content-Type", "application/json");
  
          JSONObject json = new JSONObject();
  
          json.put("to", deviceToken.trim());
          JSONObject info = new JSONObject();
          info.put("title", "notification title"); // Notification title
          info.put("body", "message body"); // Notification
          // body
          json.put("notification", info);
          try {
              OutputStreamWriter wr = new OutputStreamWriter(
                      conn.getOutputStream());
              wr.write(json.toString());
              wr.flush();
  
              BufferedReader br = new BufferedReader(new InputStreamReader(
                      (conn.getInputStream())));
  
              String output;
              System.out.println("Output from Server .... \n");
              while ((output = br.readLine()) != null) {
                  System.out.println(output);
              }
              result = "OK";
          } catch (Exception e) {
              e.printStackTrace();
              result = "BAD";
          }
          System.out.println("GCM Notification is sent successfully");
  
          return result;
  
      }
  
      public static void main(String [] args){
          try {
              PushNotifictionHelper.sendPushNotification("ep51x3Ckmig:APA91bG4PdoJC7zGlV0JPmCA49jmqJCkeSPH1QzF9byxdH1nRlFOVyAi9ppO2ygoSpp8s44o1oGO8n-HCJDB_oZAZ6WCwFD2a9yAFmKIpKhmPXakeLf-ktqPnzwf-GFziv7_nMdVPIci");
          } catch (IOException e) {
              e.printStackTrace();
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>And when I run this in console I see</p>
  
  <pre><code>java.io.IOException: Server returned HTTP response code: 401 for URL: https://fcm.googleapis.com/fcm/send
  </code></pre>
  
  <p>an AUTH_KEY_FCM I get from web from this:</p>
  
  <pre><code>Klucz interfejsu Web API AIzaSyD63pfTvnwhe9WVuIe.........
  </code></pre>