2017-04-14 133 views
0

我想發送消息給所有設備,但我只發送到一個設備,這是我如何發送消息:我只能發送消息給一個用戶,當我在我的方法中刪除此消息以發送消息:Java Firebase雲消息。發送消息給所有

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

     } 
    } 
+0

見http://stackoverflow.com/questions/37634563/fcm-firebase -cloud-messaging-how-to-send-to-all-phones –

回答

2

火力地堡支持所謂topics

所以,你可以發送郵件到一個主題,所有訂閱該主題將獲得推動的設備。

您可以擁有一個名爲all的主題,然後向每個設備註冊。

這裏是你如何註冊

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

然後,你可以解僱通知,該主題和所有用戶將得到它。

然後用

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

替換該行

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

在這種情況下,你的主題名稱是all

2

您可以訂閱所有設備到同一主題,併發送消息到主題。 通過這種方式,您甚至不需要跟蹤服務器中的令牌ID。

檢查DOX這裏:

Subscribe the client app to a topic

+0

請注意,該主題需要一段時間才能註冊。所以它不會立即工作。也許你有一天或類似的事情。 – Juan