Google Cloud Messaging(GCM)
As per Google's Documentation "Google Cloud Messaging for Android is a service that helps developers to send data from server to user's Android Application on Android devices". Using this service you can send your data in a timely manner to update user about new information, services, updates etc.
Integrating Google Cloud Messaging in your Android device provides the more good way to pass information to the user and it saves lots of battery.
Some Basic Concepts of Google Cloud Messaging through SQL Server with C#
In this tutorial, I used C# as server side programming language and SQL Server as database and to code, in a C# I used Visual Studio 2013 as an Integrated Development Environment(IDE).
If you want to know more about GCM so please visit the official document of Google here is a link
There is a simple process to learn how GCM work. Below are the steps :
Step 1: When we install any Any Android application so it will go to GCM for registration and provide Application id to GCM.
Step 2: On successful registration, GCM provides registration id to user Android device.
Step 3: After that our Android device sends this registration id to our server and we are stored that registration id in our database i.e. SQL Server to send notification purpose.
- When we want to send the notification to a user so we can take user's registration id along with our message and send to GCM Server.
- After that rest of the work is done by GCM to send the particular message to a particular user with the help of registration id.
Registering with Google Cloud Messaging
Below are the steps to registering google for GCM
Step 1: Go to Google API's console page and login with your google account and create new project (if you already created so it will take you to dashboard)
Step 2: After creating a project you can see the project id in URL.
Step 3: Go to Mobile API's section and select Google Cloud Messaging from the list.
Step 4: Click on it and enable this API.
Step 5: After this click on Credentials which is coming below.
Step 6: Provide credentials to your project by adding API name in first dropdown i.e. Google Cloud Messaging and Android in your second dropdown and click what credentials do I need
Step 7: After this create an API key by proving name and package name i.e. (your android package name) and SHA-1 certificate fingerprint.
To get a SHA-1 certificate fingerprint so please run this command from cmd and you will get this key.
$ key tool -list -v -keystore mystore.keystore
After putting all this press create API key and you will get your API key please copy this API key.
Next set is to create database and table where you can store Application id of the user to send push notification from the server.
And finally, you can use visual studio to send the request to a GCM for push notification.
Create new project in visual studio
Step 1: Open visual studio (i am using vs 2013 steps are same) to learn how to create new project in visual studio so please visit my last blog "How to create .net project in visual studio"
Step 2: Add new web form in current project and design front end as per your requirement by using this code you can set title, description, an image of a notification.
Step 3: Copy and paste below code and call that method from the button click and fill rest of the fields which is useful.
public void Pushnotification(string registrationid, string titletext, string description, string ImagePath, int ID)
{
string regid = registrationid;
var applicationid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var senderid = "xxxxxxxxxxxxx";
var value = titletext;
var _description = description;
string _imgpath = imgpath;
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));
}
Step 2: Add new web form in current project and design front end as per your requirement by using this code you can set title, description, an image of a notification.
Step 3: Copy and paste below code and call that method from the button click and fill rest of the fields which is useful.
public void Pushnotification(string registrationid, string titletext, string description, string ImagePath, int ID)
{
string regid = registrationid;
var applicationid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var senderid = "xxxxxxxxxxxxx";
var value = titletext;
var _description = description;
string _imgpath = imgpath;
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}", senderid));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.title="
+ value + "&data.img=" + _imgpath + "&data.desc=" + _description + "&data.ID=" + ID + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + regid + "";
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();
Registrationid is a user device id which we can store in the database.
titletext is a title of your notification.
description of your notification.
imagepath is a path of your image which you want to send through push notification.
id is a unique id of your client.
Applicationid is your API key which you are generated in google API's.
SenderID is your project id which you are generated in google API's.
If you have followed above procedure correctly so now you are able to send Android Push Notification through Google Cloud Messaging using .Net.
titletext is a title of your notification.
description of your notification.
imagepath is a path of your image which you want to send through push notification.
id is a unique id of your client.
Applicationid is your API key which you are generated in google API's.
SenderID is your project id which you are generated in google API's.
If you have followed above procedure correctly so now you are able to send Android Push Notification through Google Cloud Messaging using .Net.
Please run this project and I'll be happy to solve the errors if you got any.
KEEP CALM and READ ON!!!
Ankur Omar