Android Push Notification ASP.NET C#
The below code will send push notification to an android phone using ASP.NET C#
string GoogleAppID = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”; //API Key
string DeviceId= “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”; //Device Token
string msg = “Message from ASP.NET”
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}”, GoogleAppID));
String collaspeKey = Guid.NewGuid().ToString(“n”);
String postData = string.Format(“registration_id={0}&data.msg={1}&collapse_key={2}”, DeviceID, msg, collaspeKey);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
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 = newStreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tResponse.Close();
🙂
how to get API Key
Please look in to this
http://developer.android.com/google/gcm/gs.html