簡體   English   中英

Java Firebase雲消息。 發送消息給所有人

[英]Java Firebase cloud message . Send message to all

我想向所有設備發送消息,但是我只向一個設備發送消息,這就是我發送消息的方式:當我刪除發送消息的方法時,我只能向一個用戶發送消息:

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

        }
    }

您可以為所有設備訂閱相同的主題,然后向該主題發送消息。 這樣,您甚至無需跟蹤服務器中的令牌ID。

在此處檢查dox:

為客戶端應用程序訂閱主題

Firebase支持所謂的topics

因此,您可以向主題發送消息,訂閱該主題的所有設備都將獲得推送。

您可以有一個名為all的主題,然后將每個設備注冊到該主題。

這是您的注冊方式

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

然后,您只需向該主題發出通知,您的所有用戶都可以獲取它。

然后替換這條線

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

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

在這種情況下,您的主題名稱為all

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM