【问题标题】:Sending bulk push notifications using FCM in C#?在 C# 中使用 FCM 发送批量推送通知?
【发布时间】:2017-04-06 09:35:18
【问题描述】:

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

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

c#代码

   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;





    }

【问题讨论】:

标签: c# firebase firebase-cloud-messaging


【解决方案1】:

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


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

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

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

只需替换

to = deviceId

类似

registration_ids = deviceIds

其中deviceIds 是一个令牌数组。

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

【讨论】:

  • 您的回答(一年前)与旧版 API 有关,那么新的 V1 HTTP API 呢?因为registration_ids参数已经不可用了。
  • @MauroPiccotti AFAICT, registration_ids 不再受支持,使用主题是发送到多个令牌的方法。
  • 问题是主题并不适合所有事情,我的问题是:stackoverflow.com/questions/49193051/…
猜你喜欢
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 2022-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
相关资源
最近更新 更多