2017-06-29 37 views
1

私はサーバーにGoogle FirebaseのPOSTをリクエストしようとしています。私は文書ガイドラインに従ったが、それはうまくいかなかった。Google Firebase HTTP投稿

私の送信メッセージ機能は以下のものです。

private static void sendMsg() throws IOException 
{ 
    String url = "https://fcm.googleapis.com/fcm/send"; 
    URL obj = new URL(url); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

    con.setRequestMethod("POST"); 
    con.setRequestProperty("Content-Type", "application/json"); 
    con.setRequestProperty("Authorization", "key=XXXX"); 

    JSONObject msg=new JSONObject(); 
    msg.put("message","test8"); 

    JSONObject parent=new JSONObject(); 

    parent.put("to", "XXXXX"); 
    parent.put("data", msg); 

    con.setDoOutput(true); 

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

} 

応答コードは「411」で、メッセージは「長さが必要」です。 私もコンテンツの長さを設定しようとしましたが、結果は同じでした。

私は間違っていますか?

答えて

2

設定はすべて完了していますが、データは書き込まれていません。表示されたステートメントを追加してください:

con.setDoOutput(true); 
// Added 
OutputStreamWriter os = new OutputStreamWriter(con.getOutputStream()); 
os.write(parent.toString()); 
os.flush(); 
os.close(); 
+0

ありがとうございました。 –