如何在单个fcm请求中向多个设备发送fcm通知

时间:2017-01-30 09:15:24

标签: android notifications

我想在单个fcm请求中向多个设备发送通知。 我的通知文本对于所有设备都是相同的。我必须同时向所有用户发送超过10000个通知,并且文本是相同的,所以我想以最小fcm请求发送所有通知。 我正在使用c#asmx服务。 听到是我的代码。

string regid =" fcm_reg_id1,fcm_reg_id2"像这样。

string applicationID =" abcd&#34 ;;

字符串SENDER_ID =" 123456&#34 ;;

            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;
                }
            } 

2 个答案:

答案 0 :(得分:7)

由于FCM does not allow在发送邮件时指定了超过1000个注册ID:

  

此参数指定设备列表(注册令牌,或   ID)接收多播消息。它必须包含至少1和at   大多数1000个注册令牌。

您只有选项是向topic

发送消息

答案 1 :(得分: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();


                }
            }
        }
    }