0

I want to send push notification from ASPNET server with firebase, however when I use this below code that I found from this , GCM turn me with an error that says "Remote server returned error (401), not acknowledged"

Could you help me for that ? How can I send push noitification from aspnet server to android phones ?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace ConsoleApplicationFCM
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var applicationID = "AIzaSyB======UbYf6U";

                var senderId = "143===2";

                string deviceId = "APA91bHgVF0==========M9-ncvY80ZYVVeXziulC0l93p0cP4pFdnt84mY";

                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 = obj.Message,

                        //title = obj.TagMsg,

                        //icon = "myicon"

                        body = "Selam",

                        title = "Title",

                        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;

            }
        }
    }
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Check your application id and sender id . – rushank shah Jul 25 '17 at 10:02
  • Where are you getting the `applicationId` from? – AL. Jul 25 '17 at 13:18
  • Firstly I got the ApplicationId from Settings > General > App ID, it is like "1:1473===2:android:ef376c====21d0" for the first trial, I got the same error, Than I got Project ID and place it as App ID, the code again returned with the same error, – Mehmet Kahraman Jul 26 '17 at 06:26

0 Answers0