を使ってJavaに相当するものCurlでFirebase REST APIを使用して通知を送信しようとしました。https://fcm.googleapis.com/fcm/send REST Api
curl -X POST --header "Authorization: key=AIza...iD9wk" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"sound\": \"default\"}, \"to\": \"cAhmJfN...bNau9z\"}"
私はJavaで同じことをしようとしているので、私は同じことを行う簡単な方法を見つけることができず、私の試したことは何も私のモバイルエンドポイントで通知をトリガするものはありません。
これは私の最も近いアプローチです:
try {
HttpURLConnection httpcon = (HttpURLConnection) ((new URL("https://fcm.googleapis.com/fcm/send").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Authorization: key", "AIza...iD9wk");
httpcon.setRequestMethod("POST");
httpcon.connect();
System.out.println("Connected!");
byte[] outputBytes = "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"sound\": \"default\"}, \"to\": \"cAhmJfN...bNau9z\"}".getBytes("UTF-8");
OutputStream os = httpcon.getOutputStream();
os.write(outputBytes);
os.close();
// Reading response
InputStream input = httpcon.getInputStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}
System.out.println("Http POST request sent!");
} catch (IOException e) {
e.printStackTrace();
}
しかし、その後、私は得る:
java.io.IOException: Server returned HTTP response code: 401 for URL: https://fcm.googleapis.com/fcm/send
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at httpclient.test.MyHttpClientPost.sendNotification(MyHttpClientPost.java:131)
at httpclient.test.MyHttpClientPost.main(MyHttpClientPost.java:26)
よく質問を書いてくれてありがとう。簡単な問題ですが、誰かが問題を見つけて実際に助けになるために必要なすべての詳細を提供しました。もっと重要なのは、この質問が、将来他の誰かを助けるかもしれないことです。これはStackOverflowの全面的なポイントです。あなたはそれにうまく貢献しています。 [私はなぜそんなにうなずいているのですか?あまりにも間違いなくあなたがすぐに学ぶように、まあまあの質問はまれです。 –