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

2024-08-24Java开发问题
2

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

问题描述

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

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

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How vpn下载 to send data to COM free vpn PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
vpn下载 2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 vpn free Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java vpn下载 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in vpn free Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them vpn下载 consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 free vpn Java开发问题
13