0

I want to send a notification to multiple device in single FCM request. My notification text is same for all devices.

I would like to send notification to mobile devices in a batch of 100 per request. I am using c# asmx service.

Below is my code.

            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;
            }
        } 
AL.
  • 36,815
  • 10
  • 142
  • 281
milan m
  • 2,164
  • 3
  • 26
  • 40

1 Answers1

1

In order to send to specific multiple registration devices, you'll have to make use of the registration_ids parameter:

This parameter specifies the recipient of a multicast message, a message sent to more than one registration token.

The value should be an array of registration tokens to which to send the multicast message. The array must contain at least 1 and at most 1000 registration tokens. To send a message to a single device, use the to parameter.

In your code, you were using registration_id which I think is invalid. It should be registration_ids:

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

-- regIds here is an Array of Strings that contain your registration tokens.

Some previous posts that may also help (see the OPs code snippets):

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281