使用C#的Firebase推送通知

时间:2017-04-20 06:16:35

标签: c# selenium-webdriver

如何使用C#向firebase中的多个用户发送推送通知。

我使用FCM向一个用户发送通知,但是当我尝试向多个用户发送时,我的错误请求异常。

提前致谢..

string applicationID = NotificationConstants.GoogleNotificationData.GoogleAppID;
        string senderId = NotificationConstants.GoogleNotificationData.SenderId;

        WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = "application/json";

        var data = new
        {
            to = "f3fMjgIVqog:APA91bHxfhY5zbCmHqkfG2igd499DIYVVbqvi6SUT_ZeiMa9W-abce0f9tEqIupgQHiTcoU2eZKA-dZboteeWsbOsrFWdtjjPBxzI3YJTSvPJSUiSOBicBd7xd1Hb2vtioSUNvMtz0-f",                
            data = dataObject
        };

        var json = JsonConvert.SerializeObject(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;
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

您的数据应该像

string [] registration_ids = { deviceRegId };

var data= new
{
    // to = deviceRegId,                    // uncomment to notify single user
    registration_ids = registration_ids,    
    priority = "high",
    notification = new { title, body }
};