I am trying to send notifications to my server on my Android device. I use Firebase Cloud Messaging to send notifications. I can send notifications through the Firebase Console, and I get a message on my phone. However, I am trying to send a message through my server, which is not working yet.
I get the following answer when executing the code below:
"{\" message_id \ ": +58934758934758936346437}"
When we look at the Firebase documentation right here Firebase Documentation , we see that receiving message_id means that the message was sent successfully. I don't get it on my phone though.
I signed the application on the desired topic.
I run the following code:
private void test(String topic) {
try {
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.setDoOutput(true);
hc.setDoInput(true);
String message = "{\"to\": \"/topics/" + topic + "\"}";
hc.addRequestProperty("Content-Type", "application/json");
hc.addRequestProperty("Authorization", "key=SECRET");
DataOutputStream dos = new DataOutputStream(hc.getOutputStream());
dos.writeBytes(message);
dos.close();
InputStream is = hc.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
}
rd.close();
label.setText("RESPONSE: " + response.toString());
} catch (Exception ex) {
label.setText("Er ging iets mis");
ex.printStackTrace();
}
}