Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sending Push Notification to Android device using GCM C#

    • 0
    • 0
    • 0
    • 0
    • 5
    • 0
    • 0
    • 0
    • 5.63k
    Comment on it

    In one of my current project, I had to develop and integrate the Android Push notification functionality in my server side C# code.For this purpose, i used the Google Cloud Messaging service.

    Here is code for the same.


     public void SendNotification(string deviceId, string message, int badgeCount)                {
                        //API Key created in Google project 
                        string GoogleAppID = XXXX-------XX;//Google App Id
    
                        //Project ID created in Google project
                        var SENDER_ID = XXXX-------XX;//Google App Id
    
                        //Registration Id created by Android App i.e. DeviceId
                        string regID = deviceId.Trim();
    
                        WebRequest webRequest;
                        webRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
                        webRequest.Method = "post";
                        webRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
                        webRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
                        webRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
    
                        string postData = string.Format("collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message={0} &data.time={1} &data.badge={3} &data.sound={4}&registration_id={2}", message, DateTime.UtcNow, regID, badgeCount, "default");
    
                        Byte[] byteArray = Encoding.ASCII.GetBytes(postData);
    
                        webRequest.ContentLength = byteArray.Length;
    
                        Stream dataStream = webRequest.GetRequestStream();
                        dataStream.Write(byteArray, 0, byteArray.Length);
                        dataStream.Close();
    
                        WebResponse webResponse = webRequest.GetResponse();
    
                        dataStream = webResponse.GetResponseStream();
    
                        using (StreamReader streamReader = new StreamReader(dataStream))
                        {
                            String sResponseFromServer = streamReader.ReadToEnd();
                            streamReader.Close();
                            dataStream.Close();
                            webResponse.Close();
                        }
                    }
    

    In above code, few things are prerequisite which are as follows.

    deviceId.
    message.
    badgeCount.
    GoogleAppID.
    SENDERID .
    

    DeviceId is needed to identify which device the notification has to be sent to.

    Message contains the notification text.

    badgeCount is useful for showing badgecount on the app icon in IOS device.

    GoogleAppID is the App Id for the application to be picked from google project.

    SENDERID is the same as GoogleAppID.

    Notifications are sent through "https://android.googleapis.com/gcm/send" web request.

 5 Comment(s)

  • Hi Shubham,Thanks for nice comment. I could not find a way to send notification to multiple devices at one go. For this purpose, I iterated the device ids and called the method in each iteration. This way i was able to send notification to multiple device Ids.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: