在 C# 中使用 FCM 发送批量推送通知?

Rac*_*ris 2 c# firebase firebase-cloud-messaging

我正在编写 ac# 服务,它将允许我使用 FCM 发送推送通知。现在,一切正常,但我想使用 FCM 发送批量推送通知。

我希望能够一次向多个设备发送相同的消息。当前取一个注册令牌,然后经过整个过程,然后转到下一个。但是,如果我必须一次向多个号码发送推送通知,这将毫无用处。我最好的选择应该是什么?

代码

   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;





    }
Run Code Online (Sandbox Code Playgroud)

AL.*_*AL. 5

更新:对于 v1,似乎registration_ids不再支持,使用主题是最好的方法。


您可以使用registration_ids可以指定最多 1000 个注册令牌的参数:

此参数指定多播消息的接收者,将消息发送到多个注册令牌。

该值应该是要向其发送多播消息的注册令牌数组。该数组必须包含至少 1 个且最多 1000 个注册令牌。要将消息发送到单个设备,请使用 to 参数。

只需更换

to = deviceId
Run Code Online (Sandbox Code Playgroud)

registration_ids = deviceIds
Run Code Online (Sandbox Code Playgroud)

哪里deviceIds是令牌数组。

或者您可以使用Topic Messaging,只要用户订阅了您的给定主题,他们就会收到发送到该主题的消息。