I'm trying to post a notification directly from my App instead of using Firebase console. The code I wrote is this:
URL url= null;
HttpURLConnection client = null;
try{
url = new URL("https://fcm.googleapis.com/fcm/send");
client = (HttpURLConnection) url.openConnection();
client.setDoOutput(true);
client.setRequestMethod("POST");
client.setRequestProperty("Content-Type", "application/json");
client.setRequestProperty("Accept", "application/json");
client.setRequestProperty("Authorization", "key=<here my server key from firebase site");
client.setRequestProperty("project_id", "<here my sender ID from firebase site");
client.connect();
JSONObject payload = new JSONObject();
payload.put("body",news.text);
payload.put("title",news.title);
JSONObject notif = new JSONObject();
notif.put("to", "/topics/MyFirstTopic");
notif.put("notification", payload);
OutputStream outputPost = client.getOutputStream();
outputPost.write(notif.toString().getBytes("UTF-8"));
outputPost.flush();
outputPost.close();
// Read the response into a string
InputStream is = client.getInputStream();
String responseString = new Scanner(is, "UTF-8").useDelimiter("\\A").next();
is.close();
}
catch(Exception e) {
if (e.getMessage() != null) {
Log.e("SEND NOTIF TO FB", e.getMessage());
}
else {
Log.e("SEND NOTIF TO FB", e.toString());
}
}
finally {
if(client!=null) client.disconnect();
}
The code runs without errors and the response (in responseString) is apparently correct:
{
"message_id": "123456789..."
}
but in Firebase Console (in Notifications page) I can't see the posted notification neither devices receive it.
What am I missing??
Note: for beeing concise I didn't post here permission settings and code for threating notification (onMessageReceive...) but I can guarantee that it works with notifications sent from Firebase Console.
The Firebase Notifications console only shows messages that were sent from the console itself. It does not show messages that you sent through the Firebase Cloud Messaging API.