The Third Party Server is *your* server and can actually be any process written in any language (for example, it can be a batch process or a cron script). The role of the 3rd party "server" is to send the message to the device.
The Server will store (or update) the registrationID received into its local database. So, eventually the server will have registrationIDs from the devices.
Your server needs to get a ClientLogin Auth token in order to talk to the C2DM servers. When it wants to push a message to the device.
For ClientLogin Auth_Token:
Click Here
To send a message to a particular device, the Server needs to POST to the C2DM Service the following 4 things:
- The accountName which will be arxxus.pushapp@gmail.com .
- An authentication Token.
- The registrationID of the device it wants to send the message.
- The message itself (Message limit 1024 Bytes)
Code:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://android.apis.google.com/c2dm/send");
//Your Authorization Token
httppost.addHeader("Authorization", "GoogleLogin auth="+authToken);
httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("registration_id",regId));
nameValuePairs.add(new BasicNameValuePair("collapse_key", "TEST"));
//.<message> is the key and Message is "Push Contact"
nameValuePairs.add(new BasicNameValuePair("data.message","Push Contact"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
// Execute HTTP Post Request
System.out.println("Executing sendMessage");
HttpResponse response = httpclient.execute(httppost);
System.out.println(response.getStatusLine());
if (response.getEntity() != null)
{
System.out.println(EntityUtils.toString(response.getEntity()));
}
This code work properly on Apache Tomcat Server but while deploying this code on the Google App Engine you will get the Restricted class issues..So if you are thinking of deploying on Google App Engine Try this...
// Send a sync message to this Android device.
URL url = new URL("https://android.apis.google.com/c2dm/send");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Authorization", "GoogleLogin auth="+Constants.authToken);
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Accept", "text/html, */*");
urlConnection.setRequestProperty("Connection", "keep-alive");
//Send MEssage
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append("registration_id"). append("=").append("YOUR_REGISTRATION_ID");
postDataBuilder.append("&").append("collapse_key").append("=").append("TEST");
postDataBuilder.append("&").append("data.message").append("=").append(message);
byte[] postData = postDataBuilder.toString().getBytes(URLEncoder.encode("UTF-8"));
//Executing sendMessage
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(postData);
outputStream.close();
int responseCode = urlConnection.getResponseCode();
If you like this post..Please do comments..
THANKS...