2017-04-14 9 views
0

私はすべてのデバイスにメッセージを送信したいが、1つのデバイスにしかメッセージを送信しない:メッセージを送信するために自分のメソッドでこれを削除すると、1人のユーザーにメッセージを送信できる:Java Firebaseクラウドメッセージ。すべてにメッセージを送信

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

私はこの行があるときにメッセージを送信しません。私は1人のユーザーにメッセージを送信します。私はどのようにメッセージを各自に送ることができますか?

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を参照してください。この場合には

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

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

この行を置き換えます-cloud-messaging-how-to-all-phones-phones –

答えて

2

Firebaseだから、あなたが話題とプッシュを取得しますそのトピックにサブスクライブするすべてのデバイスにメッセージを送ることができます

topicsと呼ばれるものをサポートしています。

トピックはallとし、各デバイスを登録することができます。ここで

は、あなたが次にあなたがちょうどそのトピックに通知を発射することができますし、すべてのユーザーがそれを取得します

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

を登録する方法です。

その後、あなたのトピック名がall

2

すべてのデバイスを同じトピックに登録して、そのトピックにメッセージを送信できます。 このようにして、サーバーのトークンIDを追跡する必要はありません。 DOXここ

チェック:

Subscribe the client app to a topic

+0

トピックが登録されるまでに時間がかかることに注意してください。それですぐにはうまくいかないでしょう。多分あなたは、ある日かそれと何かを待っています。 – Juan

関連する問題