Today we are going to be talking about a service that Google provides called Cloud to Device Messaging on Android. In a nutshell, it is a way for servers to notify mobile applications to contact the server in order to get updated data. First, let us take a look at how the entire process works.

From the above image you can see the various different steps you need to take in order to setup and then have your web server send a notification to your application. Let's take a look at this in more detail.
1. Your application needs to send an intent to Google's C2DM Server. This intent requires certain information like the Sender ID (usually an e-mail address) and the Application's ID (package name of the application). With this Google's C2DM Server will send back an intent with either a failure message or the Registration ID.
2. Your application then needs to send the Registration ID to your Web server so that it can save this information for later use. These Registration ID's can be invalidated so your application needs to take this into account.
From the application's point of view, other than receiving the message, there is nothing else that needs to be done. The other steps of this process are on the Web server side.
3. Your web server then needs to use the Sender ID and password to retrieve a Client Login Authentication from Google's Client Login server. With this you now have all of the information needed to send a message to your application.
4. Your web server will then use your Registration ID and Client Login Authentication with the intended message to Google's C2DM server. After this Google will then handle sending the notification to the application and you just have to make sure your application does the required action when receiving the message.
This project can be split into two mini projects the first having the Android application to complete steps one and two and the second having a 3rd party web server for steps three and four. There are many examples out there on how to complete steps one and two. One example that I found easy to use is located
here. On the other hand, there is not many examples on how to setup the 3rd party web server. So let's take a look at one way to do it.
Web ServerWe will be using php, a mysql plugin, and a curl plugin. From the above steps we see that we first need to have a page where the application can send the Registration ID to the web server. This is where the mysql comes into play by taking the Registration ID and the Device Name and storing it away for use later. The next step is to retrieve the Client Login Authentication. This is done by a simple POST using curl within php.
1$account = ""; // Account
2$pass = ""; // Password
3$src = ""; // Project Name
4
5$post_params = array ( "Email" => $account, "Passwd" => $pass, "accountType"=>"GOOGLE", "source" => $src, "service" => "ac2dm" );
6
7$x = curl_init("https://www.google.com/accounts/ClientLogin");
8curl_setopt($x, CURLOPT_HEADER, 1);
9curl_setopt($x, CURLOPT_POST, 1);
10curl_setopt($x, CURLOPT_POSTFIELDS, $post_params);
11curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
12$data = curl_exec($x);
13curl_close($x);
14$response = $data;
Assuming that you don't receive any errors from this message the response will contain a Client Login Authentication key. If you want to understand the different parameters of this POST take a look
here. You will then use this along with the Registration ID to send another POST to Google's C2DM server which will handle sending the message to your device.
1$reg_id = ""; // Registration ID
2
3$device_id = "1"; // Used in case there is multiple messages being sent
4
5$headers = array('Authorization: GoogleLogin auth=' . $authKey);
6$data = array('registration_id' => $reg_id, 'collapse_key' => 'ck_' . $device_id, 'data.arg' => $message);
7
8$ch = curl_init();
9curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
10curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
11curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
12curl_setopt($ch, CURLOPT_POST, true);
13curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
14curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
15
16$response = curl_exec($ch);
For more information, on the different parameters of this POST please take a look
here. With this taken care of the Google C2DM server will handle sending it to your application.