2

i am trying to send a notification to a mobile device from firebase i have created a project in firebase console and using the following block of code to do that

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;

        }

But i am Getting "The remote server returned an error: (401) Unauthorized." Can any one please correct me where i am doing wrong

Thanks in advance Srinivas.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
srinivas challa
  • 193
  • 6
  • 14
  • 1
    You probably got your code example copied straight from [here](https://stackoverflow.com/a/39778368/265165)? It was reported in the comments that `401` errors seem to appear... I'd rather try [this other example](https://stackoverflow.com/a/48149994/265165) on this topic, it seems updated and more reliable. Give it a try. I haven't tested it. – thmshd Feb 02 '18 at 14:26

0 Answers0