Java Firebase cloud message . Send message to all -
i want send message device send 1 device how send message : can send message 1 user , when remove @ method send message:
json.put("to", tokenid.trim());
a message not send nobody when have line send message 1 user . how can send message every 1 ?
static void send_fcm_notification(string tokenid, string server_key, string message) { try { url url = new url(fcm_url); // create connection. httpurlconnection conn; conn = (httpurlconnection) url.openconnection(); conn.setusecaches(false); conn.setdoinput(true); conn.setdooutput(true); //set method post or conn.setrequestmethod("post"); //pass fcm server key conn.setrequestproperty("authorization", "key=" + server_key); //specify message format conn.setrequestproperty("content-type", "application/json"); //create json object & pass value jsonobject infojson = new jsonobject(); infojson.put("title", "wiadomosc z serwera"); infojson.put("sound", "default"); infojson.put("icon", "ic_launcher"); infojson.put("body", message); jsonobject json = new jsonobject(); json.put("to", tokenid.trim()); json.put("notification", infojson); outputstreamwriter wr = new outputstreamwriter(conn.getoutputstream()); wr.write(json.tostring()); wr.flush(); int status = 0; if (null != conn) { status = conn.getresponsecode(); } if (status != 0) { if (status == 200) { //success message bufferedreader reader = new bufferedreader(new inputstreamreader(conn.getinputstream())); system.out.println("android notification response : " + reader.readline()); } else if (status == 401) { //client side error system.out.println("notification response : tokenid : " + tokenid + " error occurred : 401"); } else if (status == 501) { //server side error system.out.println("notification response : [ errorcode=servererror ] tokenid : " + tokenid); } else if (status == 503) { //server side error system.out.println("notification response : fcm service unavailable tokenid : " + tokenid); } } } catch (malformedurlexception mlfexception) { // prototcal error system.out.println("error occurred while sending push notification!.." + mlfexception.getmessage()); } catch (ioexception mlfexception) { //url problem system.out.println("reading url, error occurred while sending push notification!.." + mlfexception.getmessage()); } catch (jsonexception jsonexception) { //message format error system.out.println("message format, error occurred while sending push notification!.." + jsonexception.getmessage()); } catch (exception exception) { //general error or exception. system.out.println("error occurred while sending push notification!.." + exception.getmessage()); } }
firebase supports called topics
so can send message topic , devices subscribed topic push.
you can have topic called all
, register each device that.
here how register
firebasemessaging.getinstance().subscribetotopic("all");
then can fire notifications topic , users it.
then replace line
json.put("to", tokenid.trim());
with
json.put("to", "/topics/your-topic-name");
in case topic name all
Comments
Post a Comment