Я пытаюсь отправить уведомление Push на мое устройство Android с новой службой Firebase. Я зарегистрировал и установил приложение, также я поместил весь код, необходимый для получения уведомления в приложении 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 = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
И это результат того, что я возвращаюсь со своих серверов:
{"multicast_id":6602141464107786356,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
К сожалению, просто удаление тега "to" не работает, я получаю код 400, который был возвращен. Я прочитал, что мне нужно зарегистрировать устройство, отправить идентификатор устройства на сервер и сохранить его там, а затем перебрать все зарегистрированные устройства на сервере для отправки сообщения. Нет ли более простого способа, просто отправить сообщение всем устройствам, как в консоли?
Ваша помощь очень ценится, так как я пытался заставить ее работать весь день = (
С уважением, Dustin