Click here to Skip to main content
Click here to Skip to main content

Android GCM Push Notification

, 6 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Android GCM Push Notification

Introduction

In this tip, I will try to explain how we can integrate a push notification service for Android using ASP.NET and C#. We all know that mobile applications are booming the market trend. Some custom mobile applications use the push notification service to give updates to application users. Here I will explain how we can use Google’s GCM push notification service.

Using the Code 

Class file "AndroidGCMPushNotification.cs":

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized;

public class AndroidGCMPushNotification
{
	public AndroidGCMPushNotification()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string SendNotification(string deviceId, string message)
    {
        string GoogleAppID = "google application id";        
        var SENDER_ID = "9999999999";
        var value = message;
        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));

        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
        
        string postData = "collapse_key=score_update&time_to_live=108&
        delay_while_idle=1&data.message=" + value + "&data.time=" + 
        System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
        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();
        
        tReader.Close();
        dataStream.Close();
        tResponse.Close();
        return sResponseFromServer;
    }
}

You can call SendNotification function by passing device Id and Message.

AndroidGCMPushNotification apnGCM = new AndroidGCMPushNotification();

string strResponse =
apnGCM.SendNotification("17BA0791499DB908433B80F37C5FBC89B870084B",
"Test Push Notification message ");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mukund Thakker
Technical Lead
India India
Software Developer
MCTS - .NET Framework 4.0, Web Applications
 
Blog : http://thakkermukund.wordpress.com
Twitter@thakkermukund
 
Don't code today, what you can't debug tomorrow!
Everything makes sense in someone's mind
Follow on   Twitter

Comments and Discussions

 
Question401 Error [modified] Pinmembertundrov26-Sep-14 4:38 
QuestionERROR PinmemberPrashant77220-Aug-14 20:12 
QuestionID's mess Pinmemberleos7930-Jun-14 20:20 
AnswerRe: ID's mess Pinmemberleos7930-Jun-14 21:13 
QuestionWebException Pinmembernugayem19-Jun-14 5:11 
AnswerRe: WebException Pinmembernugayem19-Jun-14 21:47 
QuestionError=MissingRegistration PinmemberDurul CAMLI18-Jun-14 21:31 
QuestionException PinmemberMember 107187335-Jun-14 3:13 
AnswerRe: Exception PinprofessionalMukund Thakker5-Jun-14 19:44 
Question"&registration_id" missing in code example PinmemberLoupias24-Apr-14 3:40 
QuestionSchedule Push Notifications Pinmembernano2k25-Sep-13 11:05 
Questionhow to get deviceId Pinmemberashish.shedge26-Aug-13 19:54 
QuestionError=MissingRegistration PinmemberMember 780420812-Aug-13 23:01 
AnswerRe: Error=MissingRegistration PinmemberMukund Thakker19-Aug-13 0:18 
GeneralRe: Error=MissingRegistration PinmemberGunjan Dave10-Feb-14 1:47 
GeneralRe: Error=MissingRegistration PinprofessionalMukund Thakker10-Feb-14 18:18 
GeneralRe: Error=MissingRegistration PinmemberMember 1096283021-Jul-14 23:31 
Questionthanks PinmemberAlmostafa Said Elsisy23-May-13 1:27 
QuestionMissingRegistrattion PinmemberMember 940212010-May-13 2:55 
QuestionNull Messages are sending PinmemberRAMESHVELAYUDHAN1-Apr-13 19:18 
AnswerRe: Null Messages are sending PinmemberMember 102519494-Sep-13 21:18 
GeneralRe: Null Messages are sending PinmemberRAMESHVELAYUDHAN8-Oct-13 18:37 
GeneralRe: Null Messages are sending PinmemberMukund Thakker8-Oct-13 18:51 
Bugerror occurred while processing the request to user device Pinmemberkareemgamal10-Sep-12 3:26 
GeneralRe: error occurred while processing the request to user device PinmemberHugo Saraiva28-Feb-13 8:20 
QuestionAn error occurred while processing your request. Pinmemberkareemgamal10-Sep-12 3:25 
AnswerRe: An error occurred while processing your request. PinmemberHugo Saraiva28-Feb-13 8:20 
GeneralRe: An error occurred while processing your request. PinmemberRAMESHVELAYUDHAN1-Apr-13 19:14 
GeneralRe: An error occurred while processing your request. PinmemberAlmostafa Said Elsisy22-May-13 9:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 6 Aug 2012
Article Copyright 2012 by Mukund Thakker
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid