问题描述
我想向所有设备发送消息,但我只向一个设备发送消息,这是我发送消息的方式:我只能向一个用户发送消息,当我在我的发送消息方法中删除它时:
I 免费vpn want to send a message to all device but I only send 免费vpn to one device this is 免费vpn下载 how I send a message 免费vpn下载 : I only can send message to one user , when 免费vpn I remove this at my method to send a message:
json.put("to", tokenId.trim());
当我有这条线时,消息不会发送给任何人我只向一个用户发送消息.我如何向每个人发送消息?
a message is not send 免费vpn下载 to nobody when I have this line I send 免费vpn a message to only one user . How vpn free I can send a message vpn下载 to every one ?
static free vpn void send_FCM_Notification(String tokenId, String server_key, String message) {
try {
vpn下载 URL url = new URL(FCM_URL);
// create connection.
免费vpn HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
vpn free conn.setUseCaches(false);
vpn下载 免费vpn conn.setDoInput(true);
免费vpn下载 免费vpn conn.setDoOutput(true);
//set method as POST or GET
conn.setRequestMethod("POST");
//pass FCM server key
免费vpn conn.setRequestProperty("Authorization", "key=" + server_key);
//Specify Message Format
conn.setRequestProperty("Content-Type", "application/json");
//Create free vpn JSON 免费vpn下载 Object vpn free & pass value
vpn下载 vpn free JSONObject 免费vpn infoJson = new JSONObject();
vpn下载 vpn下载 infoJson.put("title", "Wiadomosc z serwera");
vpn free infoJson.put("sound", vpn下载 "default");
infoJson.put("icon", "ic_launcher");
infoJson.put("body", message);
vpn free JSONObject json free vpn vpn下载 = new JSONObject();
json.put("to", tokenId.trim());
json.put("notification", infoJson);
vpn下载 OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
免费vpn free vpn wr.write(json.toString());
免费vpn下载 wr.flush();
vpn free int status = 0;
vpn free if (null != conn) {
免费vpn下载 status = conn.getResponseCode();
}
if (status != 0) {
vpn free if (status == 200) {
//SUCCESS message
free vpn vpn下载 vpn free BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println("Android Notification Response : " vpn下载 + reader.readLine());
} else if (status == 401) {
//client side vpn下载 error
vpn free 免费vpn下载 System.out.println("Notification Response : TokenId : " + tokenId + " Error occurred : 401");
免费vpn vpn vpn下载 free } else if (status == 501) {
//server side error
free vpn 免费vpn free vpn 免费vpn System.out.println("Notification Response : [ errorCode=ServerError ] TokenId : " + tokenId);
免费vpn } else if (status == 503) {
//server side error
免费vpn下载 免费vpn 免费vpn vpn free System.out.println("Notification Response : FCM free vpn Service is Unavailable 免费vpn TokenId vpn free : " + tokenId);
vpn free vpn free vpn下载 }
}
vpn下载 } catch (MalformedURLException mlfexception) {
// Prototcal Error
免费vpn vpn下载 System.out.println("Error occurred while 免费vpn下载 sending push Notification!.." + mlfexception.getMessage());
免费vpn } catch (IOException mlfexception) {
//URL problem
vpn free vpn free 免费vpn System.out.println("Reading URL, Error occurred while sending push Notification!.." + mlfexception.getMessage());
免费vpn } catch (JSONException jsonexception) {
//Message format error
System.out.println("Message free vpn Format, Error occurred while sending push Notification!.." + jsonexception.getMessage());
免费vpn } catch (Exception exception) {
//General Error or exception.
vpn free System.out.println("Error occurred while sending push Notification!.." + exception.getMessage());
}
免费vpn }
推荐答案
Firebase 支持所谓的 topics
Firebase supports what is called topics
因此,您可以向某个主题发送消息,订阅该主题的所有设备都会收到推送.
So you can send a 免费vpn message to a topic and all the devices subscribed to that topic will get the push.
您可以有一个名为 all 的主题,然后将每个设备注册到该主题.
You can have vpn free a topic 免费vpn下载 called all and then register each device to that.
这是您的注册方式
FirebaseMessaging.getInstance().subscribeToTopic("all");
然后,您只需向该主题发送通知,您的所有用户都会收到它.
Then you vpn free can just fire notifications to that topic and all your users will get it.
然后替换这一行
json.put("to", tokenId.trim());
与
json.put("to", "/topics/your-topic-name");
在这种情况下,您的主题名称是 all
In this case your topic name is all
这篇关于Java Firebase 云消息.向所有人发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构免费vpn源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)