Home > ASP.NET > Android Push Notification ASP.NET C#

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();

🙂

Advertisements
  1. nishantha
    October 15, 2014 at 12:07 pm

    how to get API Key

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: