如何使用.Net为用户细分和主题实施Firebase通知API

时间:2017-01-13 10:51:44

标签: c# asp.net-mvc firebase firebase-cloud-messaging

以下代码我用于发送单个设备的通知及其正常工作。但是我希望发送特定客户群的通知 应用。

WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = "application/json";
        var objNotification = new
        {
            to = "Device_Token_Id",
            notification = new
            {
                title = "Notification Title",
                body = "Notification message"

            },

            priority = "high"
        };
        string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);

        Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);

        tRequest.Headers.Add(string.Format("Authorization: key={0}", "Server_Api_Key"));
        tRequest.Headers.Add(string.Format("Sender: id={0}", "Sender_Id"));
        tRequest.ContentLength = byteArray.Length;
        tRequest.ContentType = "application/json";
        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 responseFromFirebaseServer = tReader.ReadToEnd();

                        Console.WriteLine(responseFromFirebaseServer);
                        Console.ReadLine();                                                  

                    }
                }

            }
        }

如果有人知道如何实施此API以便为特定用户群或每个人发送通知,请告诉我。

1 个答案:

答案 0 :(得分:1)

假设您的意思是Firebase控制台中提供了用户细分,那么答案就是您无法从Firebase Cloud Messaging HTTP API中定位它们。此时,这仅限于Firebase控制台。