[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; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)