Saturday 3 March 2012

C2DM Push notification for Android


Cloud to device messaging
Android Cloud to Device Messaging (C2DM) is a service that helps developers send data from servers to their applications on Android devices. The service provides a simple, lightweight mechanism that servers can use to tell mobile applications to contact the server directly, to fetch updated application or user data. The C2DM service handles all aspects of queuing of messages and delivery to the target application running on the target device. In a C2DM you have three involved parties. The application server which wants to push messages to the Android device, Googles C2DM servers and the Android app. The program on the application server can be written in any programming language, e.g. Java, PHP, Python, etc
When the application server needs to push a message to the Android application, it sends the message via an HTTP POST to Google’s C2DM servers.
The C2DM servers route the message to the device. If the device is not online, the message will be delivered once the device is available. Once the message is received, an Broadcast Intent is created. The mobile app has registered an Intent Receiver for this Broadcast. The app is started and processes the message via the defined Intent Receiver.
C2DM messages are limited in size to 1024 bytes and are intended to inform the device about new data not to transfer it. The typical workflow is that Googles C2DM servers notify the Android app that new data is available. Afterwards the Android app fetches the data from a different server.
Android maintain an connection to the Android Marketplace. C2DM uses this existing connections to the Google servers. This connection is highly optimize to minimize bandwidth and battery consumption.
C2DM is currently still in beta and you need to apply to use it. C2DM applies a limit of approximately 200 000 messages per sender per day and is currently free of charge.
To start with C2DM you need to register a Gmail ID through which your notifications will be sent signup page

#  Polling  vrs. Push

Polling architectures, as pervasive as they are today, did not come about due to their efficiency. Whether you are maintaining a popular endpoint (Twitter), or trying to get near real-time news (RSS), neither side benefits from this architecture. Over the years we've built a number of crutches in the form of Cache headers, ETags, accelerators, but none have fundamentally solved the problem -because the client remains 'dumb' the burden is still always on the server. For that reason, it's worth paying attention to some of the technologies which are seeking to reverse this trend

PUSH architectures, are nothing more but a means to push information to the end-device without the end-device having to maintain a persistent connection with the server, thus lowering the bandwidth and resources of the solution. Live APPS Push architectures reduces the cost for Network Operators as end-devices does not need to maintain a persistent connection with the WAP Proxy.

#  Requirements

C2MD is available as of Android 2.2 and requires that Android Market application is installed on the device.
To use C2DM on the Android simulator you also need to use a Google device with API 8 or higher and to register with a Google account on the emulator via the Settings.

#  Permissions

To use C2DM in your application to have to register for the following permissions
·         com.google.android.c2dm.permission.RECEIVE
·         android.permission.INTERNET
Your application should also declare the permission "applicationPackage + ".permission.C2D_MESSAGE" with the "android:protectionLevel" of "signature" so that other applications cannot register and receive message for the application. [android:protectionLevel="signature" ] ensures that applications with request a permission must be signed with same certificate as the application that declared the permission.

#  Intent Receiver

Your application must register an intent receiver for the two intents:
·         com.google.android.c2dm.intent.REGISTRATION
·         com.google.android.c2dm.intent.RECEIVE
The receiver for "com.google.android.c2dm.intent.RECEIVE" will be called once a new message is received, while the receiver for "com.google.android.c2dm.intent.REGISTRATION" will be called once the registration code for the app is received.

Limitations

C2DM imposes the following limitations:
  • The message size limit is 1024 bytes.
  • Google limits the number of messages a sender sends in aggregate, and the number of messages a sender sends to a specific device. Please refer to the section on quotas for more information.


Enjoy.... 
Do post your doubts, queries or suggestions in this blog.


No comments:

Post a Comment