c# - 在 C#.net 中向多个设备(但不是所有设备)发送 FCM 通知

标签 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;
        }
    }

to 行的问题在于我连接多个设备的位置 我如何才能发送这些设备的通知?

谢谢

最佳答案

要将推送通知发送到多个设备,您必须使用“registration_ids”而不是包含设备 token 数组的“to”参数。限制是您通过此方法最多可以提供 1000 个设备 token 。看看这个作为引用FCM Downstream Messages

关于c# - 在 C#.net 中向多个设备(但不是所有设备)发送 FCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41698696/

相关文章:

c# - 是否有理由在现代 .NET 代码中使用 goto?

C# 从 Word 文档中检索 FormFields 并插入到文本文件中

Android 应用程序不启动服务

android - Android N之后如何获取实际屏幕尺寸?

安卓蓝牙 : knowing MTU on the GattServer side

c# - 如何将静态类从 .NET 迁移到 .NET Core?

c# - Process.Start 中的 ID 有时匹配,有时不匹配

c# - 为什么 'Int32 value__' 显示为枚举成员?

c# - C# 6 中的 null 和字符串插值

.net - 如何在 .NET 中创建自己的原始数据类型?