0

I want to send web push notification on chrome using GCM.

I am trying to send GCM request to server in asp.net in my local machine. I am getting error

The remote server returned an error: (401) Unauthorized

here is link reference link which i have followed

How to send Android push notifications via GCM on C# .Net

 public string SendGCMNotification()
    {
        string postData = "";

        string deviceId = txtRegId.Text;

        string apiKey = "AIzaSyAV-xxxxxxxxxxxxxxxxxx-xxxxxxxxxxx";

        string postDataContentType = "application/json";
        //apiKey = "AIzaSyC13...PhtPvBj1Blihv_J4"; // hardcorded
        //deviceId = "da5azdfZ0hc:APA91bGM...t8uH"; // hardcorded

        string message = txtMessage.Text;
        string tickerText = "example test GCM";
        string contentTitle = "content title GCM";
        postData =
        "{ \"registration_ids\": [ \"" + deviceId + "\" ], " +
          "\"data\": {\"tickerText\":\"" + tickerText + "\", " +
                     "\"contentTitle\":\"" + contentTitle + "\", " +
                     "\"message\": \"" + message + "\"}}";


        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

        //
        //  MESSAGE CONTENT
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        //
        //  CREATE REQUEST
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
        Request.Method = "POST";
        Request.KeepAlive = false;
        Request.ContentType = postDataContentType;
        Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
        Request.ContentLength = byteArray.Length;

        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        //
        //  SEND MESSAGE
        try
        {
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                var text = "Unauthorized - need new token";
            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                var text = "Response from web service isn't OK";
            }

            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadToEnd();
            Reader.Close();

            return responseLine;
        }
        catch (Exception e)
        {
            Response.Write(e.Message);
        }
        return "error";
    }

public static bool ValidateServerCertificate(
    object sender,
    X509Certificate certificate,
    X509Chain chain,
    SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }
satyender
  • 817
  • 4
  • 12
  • 27
  • Try adding `key=` prefix for your server key. Make sure that you are using the valid server key (seen in your Firebase Console > Cloud Messaging tab). Please proceed with using FCM instead of GCM. – AL. Mar 26 '18 at 16:27
  • now i am using FCM and i have some other problem please check it https://stackoverflow.com/q/49529644/5126523 – satyender Mar 28 '18 at 09:22

0 Answers0