Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM HTTP Request returns message_id but no message is received on Android

I'm trying to send notifications on my server to my Android device. I'm using Firebase Cloud Messaging for sending the notifications. I can send notifications via the Firebase Console and I receive the message on my phone. However, I'm trying to send a message via my server, which isn't working yet.

I'm getting the following response when I execute the code below:

"{\"message_id\":58934758934758936346437}"

When we look into the documentation of Firebase right here Firebase Documentation, we can see that receiving a message_id implies that the message has been send succesfully. I'm not receiving it on my phone though.

I did subscribe the app to the right topic.

I'm running 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();
    }
}
like image 767
Guido Avatar asked Apr 05 '17 07:04

Guido


People also ask

How does FCM work on Android?

The FCM backend receives the message request, generates a message ID and other metadata, and sends it to the platform specific transport layer. When the device is online, the message is sent via the platform-specific transport layer to the device. On the device, the client app receives the message or notification.

How do I test FCM message?

Send a test notification messageOpen the Notifications composer and select New notification. Enter the message text. Select Send test message. In the field labeled Add an FCM registration token, enter the registration token you obtained in a previous section of this guide.

Does FCM guarantee delivery?

FCM does not guarantee the order of delivery. Some typical use cases of non-collapsible messages are chat messages or critical messages. For example, in an IM app, you would want to deliver every message, because every message has different content.


2 Answers

Check the message format sent from the server.

For example, here is a JSON-formatted message in the same IM app as above, where the information is encapsulated in the data key and the client app is expected to interpret the content:

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

For more query, please check this documentation: FCM Docs

like image 131
Sanjog Shrestha Avatar answered Sep 16 '22 11:09

Sanjog Shrestha


You're payload does not contain any message.

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

It only contains the recipient (to), but there is no actual message. Either send a notification or data message payload. Something like this:

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

See the available parameters for notification and data here and note that those two message payloads are handled differently. See the Receiving Messages in Android docs for more details.

like image 3
AL. Avatar answered Oct 23 '22 17:10

AL.