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();
}
}
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With