将FCM通知发送到C#.net中的多个设备(但不是所有设备)

时间:2017-01-17 13:41:01

标签: c# android .net push-notification

我在我的Android应用中使用FCM通知。 必须使用.net页面同时发送通知发送给有限数量的用户(约200个用户)

    public static void SendPushNotification()
    {

        try
        {

            string applicationID = "ABC****xyz";

            string senderId = "01*****89";

            string deviceId1 = "def****jdk";
            string deviceId2 = "lej****wka";
            string deviceId3 = "fqx****pls";

            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            var data = new
            {
                //This line is the problem
                to = deviceId1+","+deviceId2+","+deviceId3,
                notification = new
                {
                    body = "Notification Body",
                    title = "Notification Title",
                    sound = "Enabled",
                    icon = "MyIcon"

                }
            };
            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;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            string str = ex.Message;
        }
    }

行是问题,我连接多个设备的地方 我怎样才能发送这些设备的通知?

由于

1 个答案:

答案 0 :(得分:0)

要向多个设备发送推送通知,您必须使用'registration_ids'而不是'to'参数,该参数包含设备令牌数组。限制是您可以通过此方法提供最多1000个设备令牌。请查看此信息以获取参考FCM Downstream Messages