Mahan
Mahan

Reputation: 167

Send Notification to Specific Topic in Firebase in Java

Hi I'm trying to send firebase message from java code , and I have done this from the console and its working and I'm receiving Notification on My Devices , and also I have sent messages through java to specific Devices via token and its working too , and I'm having trouble sending ones to topic , I get no error and no exception from server side but nothing is received on devices. here's the code I'm using to send Requests to firebase :

 String authKey = AUTH_KEY_FCM;   // You FCM AUTH key
    String FMCurl = API_URL_FCM;

    URL url = new URL(FMCurl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization","key="+authKey);
    conn.setRequestProperty("Content-Type","application/json");

    JSONObject json = new JSONObject();
    json.put("to","/topics/all");
    JSONObject data = new JSONObject();
    data.put("message",title);
    json.put("data", data);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(json.toString());
    wr.flush();
    conn.getInputStream();
}

as you can see the inner object in the Json is Data with param "message" like the one provided on google docs , I've also tried replacing it with "notification" with "body" and "title" as its working for sending messages to specific devices via token but again not working for topics. and again its not needed to mention that everything on the android side is working cause it works sending Topic Message through firebase console

UPDATE : this method is working and I have to Handle it to actually show the notification when the message is Received .

Thanks for your helps in advance.

Upvotes: 5

Views: 2927

Answers (1)

Mahan
Mahan

Reputation: 167

this method is working now and have to handle it to actually show the notification when the message is received on the device .

Upvotes: 0

Related Questions