Saturday 1 February 2014

Google GCM Send push notification to android from server -ASP.Net

Google GCM Send notification to android Device from server for single Registration Id (using ASP.net)

https://console.developers.google.com/

From this website we will get  
1. SENDER_ID = "195123476026 "
2. API key ="AIzaSyBQgCaVwOTbsQbA5hgFbee7_mx5qvLYdI4"
  • when click on 
  • APIs ---> Google Cloud Messaging for Android click status ON 
  • and then click on Credentials then Select Create New Key --->Server Key-->Create 
  •  Application Id or API key= "AIzaSyBQgCaVwOTbsQbA5hgFbee7_mx5qvLYdI4"

 3. and will get Registration Id from Android device :

          When Android device sends sender id & application id to GCM Server,
          Google Gcm server issues Registration Id to Android Device

Create default.aspx page
Notification Message
label_Result

 Source Code (C#)


using System.Net;
using System.Text;
using System.IO;

public partial class _Default : System.Web.UI.Page

{
   string ApplicationID;
   string SENDER_ID;
   string RegId;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RegId= "APA91bEoLPR9_xxxxxxxxxx_NwulrjoxNUQ";
        ApplicationID= "AIzaSyBQgCaVwOTbsQbA5hgFbee7_mx5qvLYdI4";
        SENDER_ID= "195123476026";
        var value = TextBox1.Text; //message text box

        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));                                                                                  
        //Data post to the Server
            string postData =    
        "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="        
         + value + "&data.time=" + System.DateTime.Now.ToString() +
         "&registration_id=" +RegId + "";                            
                Console.WriteLine(postData);                                                      

         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 = new StreamReader(dataStream);                              
         String sResponseFromServer = tReader.ReadToEnd();  //Get response from GCM server  
         label_Result.Text = sResponseFromServer; //Assigning GCM response to Label text
         tReader.Close();                                                                             dataStream.Close();                                           
         tResponse.Close();                                                                                                  
       }
   }
        
 Run the application

Notification Message
You will get  Google GCM Server               Response  id=343kdhgsy4y32i42@adfgd

No comments:

Post a Comment