使用C#服务器将firebase云分散到多个用户

时间:2016-09-23 14:36:57

标签: c# firebase notifications firebase-cloud-messaging

我正在尝试使用我的c#服务器立即向多部手机发送FCM通知。我可以使用以下代码一次向一部手机发送消息,

 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是一个带有效令牌的正确数组。我不确定我做错了什么。任何帮助或建议都会非常感激。

2 个答案:

答案 0 :(得分:2)

这已经很长了,我认为这是做了很多谷歌搜索之后的正确答案。我会把它放在这里让其他开发人员通过这个bug。

问题是 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
                }
            };

答案 1 :(得分:0)

使用Firebase Device Group Messaging,您可以create a device group并在其中注册所有设备令牌,然后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!",
   }
}