如何使用firebase向asp.net mvc中的移动设备发送推送通知

时间:2018-02-02 13:40:27

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

我正在尝试从firebase向移动设备发送通知 我在firebase控制台中创建了一个项目,并使用以下代码块来执行该操作

try
        {
            var applicationID = "AIz***********************KE";
            var senderId = "52**********8";
            string deviceId = "5a1e*********55";
            WebRequest tRequest =                WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            var data = new
            {
                to = deviceId,
                notification = new
                {
                    body = "test body",
                    title = "test notification",
                  //  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;

        }

但我正在获取"远程服务器返回错误:(401)未经授权。" 任何人都可以纠正我在哪里做错了

提前致谢 SRINIVAS。

0 个答案:

没有答案