1

I'm trying to send a downstream push notification from the android client for practice only.

However, with this code:

private void send() {
    String urlString = "https://fcm.googleapis.com/fcm/send";
    JSONObject jsonObjects = new JSONObject();
    try {
        jsonObjects.put("title", titleET.getText().toString());
        jsonObjects.put("body", textET.getText().toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    RequestBody body = RequestBody.create(JSON, jsonObjects.toString());
    Request req = new Request.Builder()
            .url(urlString)
            .post(body)
            .addHeader("Authorization","key=censored")
            .build();
    try {
        Response res = client.newCall(req).execute();
        if (!res.isSuccessful()) {
            throw new UnknownError("Error: " + res.code() + " " + res.body().string());
        }
        Log.d("MainActivity", res.body().toString());
    } catch (IOException e) {
        send();
    }
}

As you can see, I'm not doing

jsonObjects.put("to","something");

That's because I don't want to send it to someone specifically, but to the whole application. If I don't put a "to" into the json, I get an error.

So, how can I send it to my whole application?

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

1 Answers1

1

You must always specify either a device token, a device group id or a topic. There is no option to send untargeted messages with Firebase Cloud Messaging.

You send notifications to all users of one app in a project from the Firebase Notifications console. But there is no public API to call this functionality from your own code.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807