-
Push notification using c#
over 8 years ago
-
over 8 years ago
You can try the below code:-
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Net; using System.Text; using System.IO; protected void Button1_Click(object sender, EventArgs e) { //Registration Id created by Android App i.e. DeviceId. string regId; regId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //API Key created in Google project var applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Project ID created in Google project. var SENDER_ID = "xxxxxxxxxxxx"; var varMessage = Hello! How are you?; WebRequest tRequest; tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send"); tRequest.Method = "post"; tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8"; tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID)); tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID)); string postDataToServer = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + varMessage + "&data.time=" + System.DateTime.Now.ToString() + "istration_id=" + regId + ""; Console.WriteLine(postDataToServer); Byte[] byteArray = Encoding.UTF8.GetBytes(postDataToServer); tRequest.ContentLength = byteArray.Length; Stream dataStream = tRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse tResponse = tRequest.GetResponse(); dataStream = tResponse.GetResponseStream(); StreamReader tReader = new StreamReader(dataStream); String sResponseFromServer = tReader.ReadToEnd(); tReader.Close(); dataStream.Close(); tResponse.Close(); }
For information visit this https://developers.google.com/cloud-messaging/
-
1 Answer(s)