FCM HTTP request returns message_id but message not received on Android

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 {
        //Setup request
        URL url = new URL("https://fcm.googleapis.com/fcm/send");
        HttpURLConnection hc = (HttpURLConnection) url.openConnection();
        hc.setDoOutput(true);
        hc.setDoInput(true);

        //Set request params
        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();

        //Get Response  
        InputStream is = hc.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
        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();
    }
}
+4
2

, .

, JSON IM, , , , :

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

: FCM Docs

+4

.

String message = "{\"to\": \"/topics/" + topic + "\"}";

(to), . notification data. - :

String message = "{\"to\": \"/topics/" + topic + "\",
    \"notification\" : {
        \"title\" : \"sample title\",
        \"body\" : \"sample body\"
    }
}";

. notification data , -. . Android.

+3

Source: https://habr.com/ru/post/1674060/


All Articles