私はJavaの残りのAPIサービスを開発しています。私はアンドロイドデバイスにプッシュ通知をする必要があります。私はそれを正しく行うにはどのように本当にわからないんだけど、私のコードは、私がアンドロイドスタジオでこのコードを実行しているから取得する私は、「サーバーのAPIキー」とuserDeviceIdKeyそのID https://developers.google.com/mobile/addから入手Androidプッシュ通知、fcm
public class FcmNotif {
public final static String AUTH_KEY_FCM = "My key";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
// userDeviceIdKey is the device id you will query from your database
public void pushFCMNotification(String userDeviceIdKey, String title, String message) 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.addProperty("to", userDeviceIdKey.trim());
JsonObject info = new JsonObject();
info.addProperty("title", title); // Notification title
info.addProperty("body", message); // Notification body
info.addProperty("image", "https://lh6.googleusercontent.com/-sYITU_cFMVg/AAAAAAAAAAI/AAAAAAAAABM/JmQNdKRPSBg/photo.jpg");
info.addProperty("type", "message");
json.add("data", info);
System.out.println(json.toString());
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
// System.out.println(url.getH);
}
public static void main(String[] args) throws Exception {
FcmNotif fcmN=new FcmNotif();
fcmN.pushFCMNotification("user id ", "myFirstMessage", "hello");
System.out.println("Done");
}
}
AUTH_KEY_FCMがある
String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
多分私は明らかに分かりませんが、間違っているのですか? 応答エラー
java.io.IOException: Server returned HTTP response code: 401 for URL: https://fcm.googleapis.com/fcm/send
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at com.gmoika.FcmNotif.makeRequest(FcmNotif.java:88)
at com.gmoika.FcmNotif.main(FcmNotif.java:111)
あなたが返信しているエラーメッセージは何ですか? –
エラーはありませんが、私はこのID "String userDeviceIdKey" –