How to send fcm notification to multiple devices in one fcm format

I want to send a notification to multiple devices in one fcm request. My notification text is the same for all devices. I need to send more than 10,000 notifications at the same time to all users, and the text will be the same, so I want to send all notifications with a minimum fcm request. I am using asmx c # service. hear my code.

string regid = "fcm_reg_id1, fcm_reg_id2", like this.

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;
                }
            } 
+6
source share
2 answers

FCM 1000 :

( ID), . 1 1000 .

topic

+7

, . ( 1000 ). :

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


                }
            }
        }
    }
0

Source: https://habr.com/ru/post/1014546/


All Articles