Google Cloud Messaging (GCM) is a service that helps developers send data from servers to their Android applications on Android devices. Google Cloud Messaging (GCM) replaces the beta version of Android Cloud to Device Messaging (C2DM).
The free service has the ability to send a lightweight message informing the Android application of new data to be fetched from the server. Larger messages can be sent with up to 4 KB of payload data.
This tutorial explains about how to send message to GCM server using .net application. Then GCM server pushes the messages to particular devices.
1. First, create new web application (File/new/website/Asp.Net Empty web site).
2. Design your page like below
Data Post Format:
{ "collapse_key": "score_update", "time_to_live": 108, "delay_while_idle": true,
"data": {
"score": "223/3",
"time": "14:13.2252"
},
"registration_ids":["4", "8", "12", "16", "25", "38"]
}
collapse_key = Message type
time_to_live = How long (in seconds) the message should be kept on
time_to_live = How long (in seconds) the message should be kept on
GCM storage if the device is offline (default:4 weeks).
delay_while_idle = It indicates that the message should not be sent
immediately if the device is idle. The server will
wait for the device to become active, and then only the
last message for each collapse_key value will be sent
data = Message Paylod.
registration_ids = A string array with the list of devices (registration IDs)
receiving the message. It must contain at least 1 and at
most 1000 registration IDs. To send a multicast message,
you must use JSON.
receiving the message. It must contain at least 1 and at
most 1000 registration IDs. To send a multicast message,
you must use JSON.
protected void SubmitButton1_Click(object sender, EventArgs e)
{
AndroidPush(); // calling android push method
}
//Android push message to GCM server method
private void AndroidPush()
{
// your RegistrationID paste here which is received from GCM server.
string regId = "APA91bG_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_V6hO2liMx-
eIGAbG2cR4DiIgm5Q";
// applicationID means google Api key
var applicationID = "AIzaSyDScBxxxxxxxxxxxxxxxxxxxpv66IfgA";
// SENDER_ID is nothing but your ProjectID (from API Console- google code)//
var SENDER_ID = "77xxxxx625";
var value = Text1.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 server
string postData =
"collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
"collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
+ value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" +
regId + "";
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(); //Get response from GCM server.
Label3.Text = sResponseFromServer; //Assigning GCM response to Label text
tReader.Close();
dataStream.Close();
tResponse.Close();
}
Note: The above code is for sending single regID. To send mutiple regID's, you can send up-to 1000 regID's to google server once or you can loop the code.
4. After clicking send button you will get response from GCM server like id=343kdhgsy4y32i42@adfgd. It means that you successfully sent message to GCM server.
You will get message in your mobile like this..
For GCM Client side tutorial click here. Download the GCM C# server side code below.
Enjoy coding.....
Enjoy coding.....