使用 C# 服务器向多个用户发送 firebase 云消息

firebase cloud messging to multiple users with C# server

我正在尝试使用我的 C# 服务器一次向多个 phone 发送 FCM 通知。我可以使用以下代码一次向一个 phone 发送消息,

 try
        {
            var applicationID = "xAxxxxxxxxxxxxxxxxxxxx6g9vOeEmw1njaivVfIx";
            var senderId = "x7xxxxxxxxxxx5x";
            string deviceId = pushToken;
            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            var msg2send = new
            {
                to = deviceId,
                notification = new
                {
                    body = msg,
                    title = "My Car Wash App",
                    icon = "myicon"
                },
                data = new
                {
                    priority = 10,
                    notice = msg
                }
            };

        var serializer = new JavaScriptSerializer();
        var json = serializer.Serialize(msg2send);

         Response.Write(json);

        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();
                        Response.Write(sResponseFromServer);
                    }
                }
            }
        }
    }

    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

效果很好,但我遍历我的令牌并一遍又一遍地调用该方法发出数百个请求,这并不好

我正在尝试发送多播消息,因为我可以传入一个包含所有令牌的数组,但我一直收到错误的请求

var msg2send = new
            {
                registration_ids = "[id1, id2]",
                notification = new
                {
                    body = msg,
                    title = "My Car Wash App",
                    icon = "myicon"
                },
                data = new
                {
                    priority = 10,
                    notice = msg
                }
            };

当我发送请求时,我确信 registration_ids 是一个带有有效令牌的正确数组。我不确定我做错了什么。非常感谢任何帮助或建议。

Firebase Device Group Messaging, you can create a device group and register all the device tokens in it, then send downstream messages to client apps 使用下面的 JSON 负载。

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{
   "to": "aUniqueKey",
   "data": {
      "hello": "This is a Firebase Cloud Messaging Device Group Message!",
   }
}

已经很长时间了,我认为这是经过大量 google 搜索后的正确答案。我会把它放在这里,以便其他开发人员可以解决这个错误。

问题是registration_ids = "[id1, id2]", // registration_ids 需要字符串数组 但是你给它分配了一个字符串。这就是为什么许多开发人员收到错误请求但又不知道原因的原因。

解决方法:尝试在var msg2send = new {...}之外创建一个数组。我测试了它,它的工作效率为 100%

string[] deviceIds = new string[] {"id1","id2","id3"};
var msg2send = new
            {
                registration_ids = deviceIds,
                notification = new
                {
                    body = msg,
                    title = "My Car Wash App",
                    icon = "myicon"
                },
                data = new
                {
                    priority = 10,
                    notice = msg
                }
            };