Tuesday, January 17, 2012

Android C2DM: Part 1 (Overview)

Today we will be exploring Google’s own cloud based push technology called C2DM (Cloud to Device Messaging) available for Android platform. C2DM helps third party Android app developers to receive push notifications sent by their application server.
Before getting into C2DM details let’s see why push is required.

Why Push is required?

Most mobile solutions have client-server architecture where mobile apps frequently need to download updated data from server. In some situations server need to push some short messages to mobile client like notification regarding new app update or event. For these kinds of features, we can use polling or push mechanism.
In polling mechanism, mobile app pings server periodically and checks if any new data is available for download. Polling is easy to implement and works well in most of the situations but tricky part is selecting the frequency for polling. Polling rarely can make data on device stale and polling frequently can create stress on server and network. This approach is not suitable for mobile devices as it uses precious network bandwidth more and so battery consumption will be more.
Other alternative is push mechanism for which we can follow two different implementation approaches. First approach is using proper push bearer, a network mechanism that can deliver unsolicited messages to devices. SMS and Blackberry’s proprietary push solution are the examples of push bearer. Second implementation approach is to simulate this technology by opening TCP connection to the push server. As we know TCP streams are full duplex (having pair of byte streams, one following each direction), server can push data to device anytime provided stream is active.
Most Smartphone platforms like iOS, Blackberry, Windows Mobile have their own push technology. Android OS was exception till its Froyo release as Android’s framework does not has support for push technology and app developers have to use open source alternatives like MQTT, Urban Airship and Deacon. With Froyo release, Android received support for Google’s own cloud based push technology known as C2DM.

C2DM Overview
C2DM is a solution to send real time notifications to user on his Android handset and most Google Services (Gmail, Market, Maps etc.) supports it. C2DM server/service takes care of all aspects of queuing and delivery of messages to target devices running developer’s target apps. C2DM also acts as an intermediary between developer’s App server and Android device.
Before moving into implementation details, we will see prerequisites for C2DM implementations, advantages and limitations of C2DM service and some guidelines for C2DM implementation.

Prerequisites

  • Android 2.2+ Device or Emulator
  • At least one logged in Google user on device/emulator
  • Application server which can talk HTTP and connect to C2DM server
  • Android application should have permissions to receive C2DM message and to make internet connection
  • Developer must register on C2DM site to use C2DM for his application and to get developer quota for C2DM messages.

Advantages

  • Application need not be running in background to receive push messages as those gets delivered to your app (Intent Receiver) by system using Intent broadcast.
  • It does not create additional burden on network as it uses existing connection to Google services.
  • Data delivery to device will always secured as device to C2DM server connection supports SSL.
  • Broken connections will be re-established using heartbeat implementation.
  • C2DM framework does not provide any built-in user interface to display the received push message content. C2DM framework provides full freedom to application so that it will have full control over the received raw message content. For example, the application might post a notification, display a custom user interface, or silently synchronize data.

Limitations

  • Push message size limit is 1K (1024 bytes)
  • C2DM framework doesn’t give guarantee about delivery or order of push messages
  • C2DM sender account will have daily limit on total number of messages sent as well as messages sent per device. Currently total limit is 200,000 messages per day.

Guidelines

  • When Google service is down your application will get HTTP error code 503 along with retry value. Your app should respect these kinds of errors and it should have advanced error-handling mechanism in place.
  • Your solution should have its own queue mechanism in place, which will be useful when Google C2DM service returns over quota error.
  • Application server should not send large data; instead, it has to send ‘tickles’ to your application telling it to download latest data from server.
  • Avoid using ‘Role Account Email ID’ on device
Part 2 of this blog will cover C2DM architecture and life-cycle in details.

Update : Google has deprecated C2DM and replaced it with GCM. Please read more about GCM here.

2 comments:

  1. Nice blog. I have some queries here.
    Is this a free service?
    Is the 200,000 is per device or per application?

    Thanks,
    Raghav.

    ReplyDelete
    Replies
    1. Its completely free service. If developer need more quota then only developer need to contact Google to get increased quota.

      200,000/day limit is applicable for sender ID (application developer's email ID) means using registered email ID 3rd party app server can send maximum 200,000 messages per day. For more details visit

      Delete