• <legend id='VJFZv'><style id='VJFZv'><dir id='VJFZv'><q id='VJFZv'></q></dir></style></legend>
    <i id='VJFZv'><tr id='VJFZv'><dt id='VJFZv'><q id='VJFZv'><span id='VJFZv'><b id='VJFZv'><form id='VJFZv'><ins id='VJFZv'></ins><ul id='VJFZv'></ul><sub id='VJFZv'></sub></form><legend id='VJFZv'></legend><bdo id='VJFZv'><pre id='VJFZv'><center id='VJFZv'></center></pre></bdo></b><th id='VJFZv'></th></span></q></dt></tr></i><div id='VJFZv'><tfoot id='VJFZv'></tfoot><dl id='VJFZv'><fieldset id='VJFZv'></fieldset></dl></div>

    1. <tfoot id='VJFZv'></tfoot>

      <small id='VJFZv'></small><noframes id='VJFZv'>

        <bdo id='VJFZv'></bdo><ul id='VJFZv'></ul>

        Java Firebase 云消息.向所有人发送消息

        时间:2024-08-24

        <small id='jy1hJ'></small><noframes id='jy1hJ'>

        <legend id='jy1hJ'><style id='jy1hJ'><dir id='jy1hJ'><q id='jy1hJ'></q></dir></style></legend>
        • <tfoot id='jy1hJ'></tfoot>
              <tbody id='jy1hJ'></tbody>
            • <bdo id='jy1hJ'></bdo><ul id='jy1hJ'></ul>

                <i id='jy1hJ'><tr id='jy1hJ'><dt id='jy1hJ'><q id='jy1hJ'><span id='jy1hJ'><b id='jy1hJ'><form id='jy1hJ'><ins id='jy1hJ'></ins><ul id='jy1hJ'></ul><sub id='jy1hJ'></sub></form><legend id='jy1hJ'></legend><bdo id='jy1hJ'><pre id='jy1hJ'><center id='jy1hJ'></center></pre></bdo></b><th id='jy1hJ'></th></span></q></dt></tr></i><div id='jy1hJ'><tfoot id='jy1hJ'></tfoot><dl id='jy1hJ'><fieldset id='jy1hJ'></fieldset></dl></div>

                  本文介绍了Java Firebase 云消息.向所有人发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想向所有设备发送消息,但我只向一个设备发送消息,这是我发送消息的方式:我只能向一个用户发送消息,当我在我的发送消息方法中删除它时:

                  I want to send a message to all device but I only send to one device this is how I send a message : I only can send message to one user , when I remove this at my method to send a message:

                  json.put("to", tokenId.trim());
                  

                  当我有这条线时,消息不会发送给任何人我只向一个用户发送消息.我如何向每个人发送消息?

                  a message is not send to nobody when I have this line I send a message to only one user . How I can send a message to every one ?

                   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 as POST or GET
                              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 is 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 支持所谓的 topics

                  Firebase supports what is called topics

                  因此,您可以向某个主题发送消息,订阅该主题的所有设备都会收到推送.

                  So you can send a message to a topic and all the devices subscribed to that topic will get the push.

                  您可以有一个名为 all 的主题,然后将每个设备注册到该主题.

                  You can have a topic called all and then register each device to that.

                  这是您的注册方式

                  FirebaseMessaging.getInstance().subscribeToTopic("all");
                  

                  然后,您只需向该主题发送通知,您的所有用户都会收到它.

                  Then you 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 云消息.向所有人发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何通过 Firebase 从用户向用户发送直接消息? 下一篇:Firebase 消息传递 - “警报太多"在三星的安卓设备上

                  相关文章

                  <i id='aay4v'><tr id='aay4v'><dt id='aay4v'><q id='aay4v'><span id='aay4v'><b id='aay4v'><form id='aay4v'><ins id='aay4v'></ins><ul id='aay4v'></ul><sub id='aay4v'></sub></form><legend id='aay4v'></legend><bdo id='aay4v'><pre id='aay4v'><center id='aay4v'></center></pre></bdo></b><th id='aay4v'></th></span></q></dt></tr></i><div id='aay4v'><tfoot id='aay4v'></tfoot><dl id='aay4v'><fieldset id='aay4v'></fieldset></dl></div>
                    <bdo id='aay4v'></bdo><ul id='aay4v'></ul>
                  <tfoot id='aay4v'></tfoot>
                • <legend id='aay4v'><style id='aay4v'><dir id='aay4v'><q id='aay4v'></q></dir></style></legend>

                  <small id='aay4v'></small><noframes id='aay4v'>