2017-01-30 32 views
6

我想在單個fcm請求中向多個設備發送通知。 我的通知文本是相同的所有devices.i必須發送更多然後10000通知在同一時間所有用戶和文本是相同的,所以我想在最低fcm請求發送所有通知。 我正在使用c#asmx服務。 聽到是我的代碼。如何將fcm通知發送到單個fcm請求中的多個設備請求

string regid =「fcm_reg_id1,fcm_reg_id2」像這樣。

string applicationID =「abcd」;

string SENDER_ID =「123456」;

  string regid="c_Z5yRoj4TY:APA91bGry2g_CIA1xaRy_LscxOvFX6YHqasKA96TjpG6yi1yytNyM5rtGL6DgxjGMSE5c74d7VdSL6W8zxO1ixVMlpVMwdgcrsGUWV0VfdbddC2XD","c_Z5yRoj4TY:APA91bGry2g_CIA1xaRy_LscxOvFX6YHqasKA96TjpG6yi1yytNyM5rtGL6DgxjGMSE5c74d7"; 

      HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send"); 

      httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; 

      httpWebRequest.Method = "POST"; 

      String collaps_key = "Score_update"; 

      string json = "collapse_key=abcd" + "&data.header=cricket&registration_id=" + regId + "&data.notificationId=" + notificationId + "&data.message=" + msg; 

      httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID)); 
      httpWebRequest.Headers.Add(string.Format("Sender: key={0}", SENDER_ID)); 

      using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) 
      { 
       //Console.WriteLine(json); 
       streamWriter.Write(json); 
       streamWriter.Flush(); 
       streamWriter.Close(); 
       using (HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse()) 
       { 
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
        { 
         var result = streamReader.ReadToEnd(); 
         Console.WriteLine(result); 
         retmsgid = result.ToString(); 
         if (retmsgid.Trim() != "") 
         { 
          ResponceString = result.ToString(); 
          string[] msgsplits = retmsgid.Split(','); 
          string[] msg1 = msgsplits[0].ToString().Split(':'); 
          ReturnMessageId = msg1[1].ToString(); 
         } 
         else 
         { 
          ReturnMessageId = "0"; 
         } 
        } 
        httpResponse.Close(); 
        httpResponse.Dispose(); 
        httpWebRequest = null; 
       } 
      } 
+0

您可以使用IMEI號碼(需要在您的後端註冊IMEI號碼)從網上執行此操作。因此,您需要使用移動設備上的網絡服務發佈消息。現在,您可以使用IMEI號碼註冊的設備從網絡上廣播您的消息。 –

+0

我想在上面的代碼中的單個請求發送多個通知,我想添加多個註冊ID併發送它。 – user3599175

回答

7

由於FCM does not allow當發送消息時,指定超過1000個註冊ID:

此參數指定裝置接收多播消息的列表(註冊標記,或 ID)的。它必須包含至少1個和最多1000個註冊令牌。

你唯一的選擇是將消息發送到topic

+0

我想一次發送消息到大約100個註冊ID(僅限移動設備)。我如何撰寫請求? –

+0

那麼,這是一個不同的[問題](http://stackoverflow.com/a/13650308/3942452) –

+0

其實我的問題是,只有。可能是我無法正確設置它。 –

0

沒有爲所有尋求發送通知,限制註冊設備的方法可能的解決方案。 (少於1000個設備)。 考慮作爲字符串裝置ID的輸入:

device_ids_input="code1,code2,code3,.."; 

使用此代碼段,以用於發送通知執行命令。

string[] deviceIds = device_ids_input.Split(','); 
    string hasSound = "1"; 
    string applicationID = "AIz------------------"; 
    string senderId = "73-------"; 


    WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); 
    tRequest.Method = "post"; 
    tRequest.ContentType = "application/json"; 
    var data = new 
    { 
     registration_ids = deviceIds, 
     data = new 
     { 
      title = messageTitle, 
      full_text = messageBody, 
      Sound = hasSound 

     } 
    }; 
    var serializer = new JavaScriptSerializer(); 
    var json = serializer.Serialize(data); 
    Byte[] byteArray = Encoding.UTF8.GetBytes(json); 
    tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID)); 
    tRequest.Headers.Add(string.Format("Sender: id={0}", senderId)); 
    tRequest.ContentLength = byteArray.Length; 
    using (Stream dataStream = tRequest.GetRequestStream()) 
    { 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     using (WebResponse tResponse = tRequest.GetResponse()) 
     { 
      using (Stream dataStreamResponse = tResponse.GetResponseStream()) 
      { 
       using (StreamReader tReader = new StreamReader(dataStreamResponse)) 
       { 
        String sResponseFromServer = tReader.ReadToEnd(); 


       } 
      } 
     } 
    }