【问题标题】:Java Firebase cloud message . Send message to allJava Firebase 云消息。向所有人发送消息
【发布时间】:2017-04-14 13:25:02
【问题描述】:

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

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

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

 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());

        }
    }

【问题讨论】:

标签: java firebase-cloud-messaging


【解决方案1】:

Firebase 支持所谓的topics

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

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

这是您的注册方式

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

然后您可以向该主题发送通知,您的所有用户都会收到它。

然后替换这一行

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

json.put("to", "/topics/your-topic-name");

在这种情况下,您的主题名称是all

【讨论】:

    【解决方案2】:

    您可以为所有设备订阅同一主题,并向该主题发送消息。 通过这种方式,您甚至不需要跟踪服务器中的令牌 ID。

    在此处查看 dox:

    Subscribe the client app to a topic

    【讨论】:

    • 请记住,主题注册需要一些时间。所以它不会立即起作用。也许你已经等了一天或类似的事情。
    猜你喜欢
    • 2017-02-07
    • 2015-01-27
    • 2020-09-09
    • 2017-01-19
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    相关资源
    最近更新 更多