1

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.

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
Maiosoft
  • 13
  • 4
  • I can run your code and receive notifications. You'll only see the notification on the client app if it is in the background. Also, as Frank explained in his answer, the notification message is not shown on the Firebase Console. You may also be aware the your server key should be a carefully guarded secret. Putting it in app code is okay for personal projects or tests, but is not safe for a production app. – Bob Snyder Apr 21 '17 at 14:58
  • You're right Bob! As I answered to Frank, it was my mistake! What do you suggest for the server key? Where or how can I store it safely and use it in my HTTP request?? – Maiosoft Apr 21 '17 at 15:45
  • Consider using [Firebase Cloud Functions](https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens) – Bob Snyder Apr 21 '17 at 15:57

1 Answers1

0

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.

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • Thank you Frank! I didn't know this... the problem was the incorrect topic name! Devices was subscribed on another topic, so no notification arrived to clients, no notificatin listed in Firebase... very confused! I'm also trying to send "segment" notification programmatically, but it seems there's no chance (http://stackoverflow.com/questions/39326251/fcm-programatically-send-push-notification-to-user-segments) Do you know a way to do this anyway? – Maiosoft Apr 21 '17 at 15:43
  • As said here: http://stackoverflow.com/a/37376757/209103 there is no public API to send messages to audiences. The Notifications console is currently the only way to accomplish that. – Frank van Puffelen Apr 21 '17 at 16:18
  • Ok, so what do you think about using a "generic" topic to which all devices running my App subscribe? I could send a notification for topic "generic" from my "Admin App" that would reach everyone? Do you find it a so bad solution? – Maiosoft Apr 24 '17 at 10:20
  • Just a detail: my App estimated audience will be no more than 200-300 devices (a closed group). Most of notifications will be for specific topics, but sometimes it will be necessary to inform all of them about trasversal events... – Maiosoft Apr 24 '17 at 10:27
  • If you want to send announcement messages, sending those to a topic that everyone subscribes to is indeed the common way to accomplish this. – Frank van Puffelen Apr 24 '17 at 13:43