Monday, 3 June 2013

Send Push Notification to android mobile using MVC,Asp.net

Send Push Notification to android mobile using MVC  Code C#


Step 1 : First create your [MVC ,Asp.net Application] .

Step 2 : Paste the below code in your application.

public void NotifyTest(string regId)
        {
            var applicationID = "xxxxxxxxxxxxxxxxxxxx";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"registration_ids\":[\"" + regId + "\"]," +
                            "\"data\": {\"title\": \"This is testing.\"}}";
                Console.WriteLine(json);
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    Console.WriteLine(result);
                }
            }
        }

Step 3  : Call the function  NotifyTest(regID) with one Register ID of mobile  parameter.

Step 4 : This way you can send push notification.

No comments:

Post a Comment

Thank you for your interest .