Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting no error but also not getting any message on my android device.

What I have tried:

C#
[HttpPost]
    public string SendGCMNotification(string deviceId)
    {

        string apiKey = "xxxxxxxxxxxxxxxxxxxxxg";
        string postData;

       

        string message = "Demo";
        string tickerText = "Demoo";
        string contentTitle = "Demor";
        string SENDER_ID = "xxxxxxx";

        postData = "{ \"registration_ids\": [ \"" + deviceId + "\" ], " + "\"data\": {\"tickerText\":\"" + tickerText + "\", " +
                 "\"contentTitle\":\"" + contentTitle + "\", " +
                 "\"message\": \"" + message + "\"}}";

        string postDataContentType = "application/json";

        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);


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


        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.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
        Request.ContentLength = byteArray.Length;

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


        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)
        {
        }
        return "error";
    }


    public static bool ValidateServerCertificate(
                                                 object sender,
                                                 X509Certificate certificate,
                                                 X509Chain chain,
                                                 SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }
Posted
Updated 21-Oct-16 18:22pm
v2
Comments
Suvendu Shekhar Giri 22-Oct-16 0:21am    
any luck with debugging? any clue?
Esle 22-Oct-16 0:52am    
no.
when I send the request it gives an id in response on my restclient.
nothing else and no affect on my android app as i already opened the app on my android device
Esle 22-Oct-16 7:36am    
May I know how can i send an image in push notification using fcm c#.
Have you opened any socket connection?
Esle 24-Oct-16 3:16am    
No @Tadish Dash

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900