2

I am writing a c# service that will allow me to send push notifications using FCM. Now, everything works fine but then I want to send bulk push notification using FCM.

I want to be able to send the same message to multiple devices at once. The current takes one registration token and then goes through the whole process and then goes to the next one. But this will be useless if I have to send push notification to many numbers at once. What should be my best option?

c# code

   public String SendPushNotification(string regtoken)
    {


            try
            {

                string applicationID = "#############3";

                string senderId = "##########";

                string deviceId = regtoken;
                WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                tRequest.Method = "post";
                tRequest.ContentType = "application/json";
                var data = new
                {
                    to = deviceId,
                    notification = new
                    {
                        body = message,
                        title = "",
                        sound = ""

                    }
                };
                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();
                                String str = sResponseFromServer;
                            }
                        }
                    }
                    status = statusmessages();
                }
            }
            catch (Exception ex)
            {
                string str = ex.Message;
                status = "failure";
            }


        return status;





    }
AL.
  • 33,241
  • 9
  • 119
  • 257
Rachel Morris
  • 117
  • 2
  • 11
  • 1
    Possible duplicate of [FCM (Firebase Cloud Messaging) Send to multiple devices](http://stackoverflow.com/questions/39547277/fcm-firebase-cloud-messaging-send-to-multiple-devices) – AL. Apr 06 '17 at 09:42

1 Answers1

5

Update: For v1, it seems that registration_ids is no longer supported and that making use of topics is the best approach.


You can either use the registration_ids parameter where you'll be able to specify up to 1000 registration tokens:

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.

Just replace

to = deviceId

to something like

registration_ids = deviceIds

where deviceIds is an array of tokens.

Or you could make use of Topic Messaging, where so long as the user is subscribed to your given topic, they would receive messages sent to that topic.

Community
  • 1
  • 1
AL.
  • 33,241
  • 9
  • 119
  • 257
  • Your answer (one year ago) is related to legacy API, what about the new V1 HTTP API? Because the registration_ids parameter is not available anymore. – Mauro Piccotti Mar 09 '18 at 11:54
  • @MauroPiccotti AFAICT, `registration_ids` is no longer supported and using topics is the way to go with sending to multiple tokens. – AL. Mar 09 '18 at 17:09
  • The problem is that topics are not good for everything, here my question: https://stackoverflow.com/questions/49193051/firebase-cloud-messaging-fcm-http-v1-api-or-legacy-http-api – Mauro Piccotti Mar 09 '18 at 20:22