我正在尝试使用新的Firebase服务向我的Android设备发送推送通知.我注册并设置了一个应用程序,我还在android应用程序中放置了接收通知所需的所有代码.通过Firebase控制台,我可以向我的应用发送通知,并收到并显示.现在我想编写一个java独立服务器,向所有设备发送通知.这是我目前的代码:
final String apiKey = "I added my key here";
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + apiKey);
conn.setDoOutput(true);
String input = "{\"notification\" : {\"title\" : \"Test\"}, \"to\":\"test\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
os.close();
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + input);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response …Run Code Online (Sandbox Code Playgroud)