Click here to Skip to main content
11,412,526 members (72,938 online)
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
Tech Lead
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

 
GeneralPlagiarism alert Pin
Sriram Sakthivel at 26-Mar-15 3:53
memberSriram Sakthivel26-Mar-15 3:53 
GeneralRe: Plagiarism alert Pin
Mukund Thakker at 26-Mar-15 20:14
professionalMukund Thakker26-Mar-15 20:14 
Questionsending a message to multiple registration_id at a time [modified] Pin
mannmadhan at 26-Feb-15 3:18
membermannmadhan26-Feb-15 3:18 
QuestionGetting also MissingRegistration Pin
Member 9531156 at 20-Jan-15 22:54
memberMember 953115620-Jan-15 22:54 
AnswerRe: Getting also MissingRegistration Pin
Mukund Thakker at 22-Jan-15 2:02
professionalMukund Thakker22-Jan-15 2:02 
SuggestionNo need to "SENDER_ID" Pin
Bebosh at 9-Dec-14 4:00
memberBebosh9-Dec-14 4:00 
QuestionCan we send push notification to bulk mobiles device tokens and get status of each mobile device token? Pin
Member 10134528 at 4-Dec-14 19:26
memberMember 101345284-Dec-14 19:26 
GeneralExcellent Work Pin
Rungta Atul at 2-Dec-14 10:13
memberRungta Atul2-Dec-14 10:13 
Question401 Error [modified] Pin
tundrov at 26-Sep-14 5:38
membertundrov26-Sep-14 5:38 
QuestionERROR Pin
Prashant772 at 20-Aug-14 21:12
memberPrashant77220-Aug-14 21:12 
QuestionID's mess Pin
leos79 at 30-Jun-14 21:20
memberleos7930-Jun-14 21:20 
AnswerRe: ID's mess Pin
leos79 at 30-Jun-14 22:13
memberleos7930-Jun-14 22:13 
QuestionWebException Pin
nugayem at 19-Jun-14 6:11
membernugayem19-Jun-14 6:11 
AnswerRe: WebException Pin
nugayem at 19-Jun-14 22:47
membernugayem19-Jun-14 22:47 
QuestionError=MissingRegistration Pin
Durul CAMLI at 18-Jun-14 22:31
memberDurul CAMLI18-Jun-14 22:31 
QuestionException Pin
Member 10718733 at 5-Jun-14 4:13
memberMember 107187335-Jun-14 4:13 
AnswerRe: Exception Pin
Mukund Thakker at 5-Jun-14 20:44
professionalMukund Thakker5-Jun-14 20:44 
Question"&registration_id" missing in code example Pin
Loupias at 24-Apr-14 4:40
memberLoupias24-Apr-14 4:40 
QuestionSchedule Push Notifications Pin
nano2k at 25-Sep-13 12:05
membernano2k25-Sep-13 12:05 
Questionhow to get deviceId Pin
ashish.shedge at 26-Aug-13 20:54
memberashish.shedge26-Aug-13 20:54 
QuestionError=MissingRegistration Pin
Member 7804208 at 13-Aug-13 0:01
memberMember 780420813-Aug-13 0:01 
AnswerRe: Error=MissingRegistration Pin
Mukund Thakker at 19-Aug-13 1:18
memberMukund Thakker19-Aug-13 1:18 
GeneralRe: Error=MissingRegistration Pin
Gunjan Dave at 10-Feb-14 2:47
memberGunjan Dave10-Feb-14 2:47 
GeneralRe: Error=MissingRegistration Pin
Mukund Thakker at 10-Feb-14 19:18
professionalMukund Thakker10-Feb-14 19:18 
GeneralRe: Error=MissingRegistration Pin
Member 10962830 at 22-Jul-14 0:31
memberMember 1096283022-Jul-14 0:31 
Questionthanks Pin
Almostafa Said Elsisy at 23-May-13 2:27
memberAlmostafa Said Elsisy23-May-13 2:27 
QuestionMissingRegistrattion Pin
Member 9402120 at 10-May-13 3:55
memberMember 940212010-May-13 3:55 
QuestionNull Messages are sending Pin
RAMESHVELAYUDHAN at 1-Apr-13 20:18
memberRAMESHVELAYUDHAN1-Apr-13 20:18 
AnswerRe: Null Messages are sending Pin
Member 10251949 at 4-Sep-13 22:18
memberMember 102519494-Sep-13 22:18 
GeneralRe: Null Messages are sending Pin
RAMESHVELAYUDHAN at 8-Oct-13 19:37
memberRAMESHVELAYUDHAN8-Oct-13 19:37 
GeneralRe: Null Messages are sending Pin
Mukund Thakker at 8-Oct-13 19:51
memberMukund Thakker8-Oct-13 19:51 
Bugerror occurred while processing the request to user device Pin
kareemgamal at 10-Sep-12 4:26
memberkareemgamal10-Sep-12 4:26 
GeneralRe: error occurred while processing the request to user device Pin
Hugo Saraiva at 28-Feb-13 9:20
memberHugo Saraiva28-Feb-13 9:20 
QuestionAn error occurred while processing your request. Pin
kareemgamal at 10-Sep-12 4:25
memberkareemgamal10-Sep-12 4:25 
AnswerRe: An error occurred while processing your request. Pin
Hugo Saraiva at 28-Feb-13 9:20
memberHugo Saraiva28-Feb-13 9:20 
GeneralRe: An error occurred while processing your request. Pin
RAMESHVELAYUDHAN at 1-Apr-13 20:14
memberRAMESHVELAYUDHAN1-Apr-13 20:14 
GeneralRe: An error occurred while processing your request. Pin
Almostafa Said Elsisy at 22-May-13 10:22
memberAlmostafa Said Elsisy22-May-13 10: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 | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 6 Aug 2012
Article Copyright 2012 by Mukund Thakker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid