Tuesday, January 31, 2012

Android C2DM: Part 2 (Lifecycle)

In Part 1 of this blog series we have seen C2DM overview. Today in this blog we will explore C2DM in details. We will see C2DM architecture and C2DM workflow.

C2DM Architecture
Below diagram explains high level C2DM architecture
Three parties are involved in this C2DM architecture 
  • Mobile device running C2DM based Android app
  • Third Party app server will send push message to device through C2DM server
  • C2DM server receives messages from third party app server and sends to mobile device/app
Different credentials passed between these parties while transferring C2DM message to authenticate involved parties and make secured data transfer between them. These credentials explained below
  • Sender ID/Email used in C2DM signup process and is associated with application’s developer
  • Application ID (package name) ensures messages are delivered to correct application
  • Registration ID issued by C2DM server is uniquely represents app running on specific device
  • Google User Account for at least one logged in user on device
  • Auth token required to get authorized access to Google services like C2DM

C2DM Workflow
Above diagram shows C2DM workflow in detail starting from C2DM registration to actual message delivery. These workflow steps grouped into three processes/categories as given below
  • C2DM setup
  • Sending message
  • Receiving message
Below sections gives more details about these three processes
C2DM Setup
  • C2DM signup - App developer has to sign up for C2DM service before implementing C2DM in his app. In signup process, he has to provide his app ID (package name) and Role Account Email ID. Generally, white listing of your account and getting developer quota is very quick. This is one time process for each application.
  • C2DM registration - Android app registers itself for C2DM service by firing registration intent (com.google.android.c2dm.intent.REGISTER). App includes additional parameters in this intent like sender (Email ID used in C2DM signup process) and app (package name).
  • Get Registration ID - Android app has to register broadcast receiver for registration intent (com.google.android.c2dm.intent.REGISTRATION). Google will send this intent upon successful registration.
  • Send registration ID to third Party App Server - Application has to send registration ID received from C2DM server to third party App server. We recommend sending device ID along with registration ID so that third party app server can update it into its database against device.
  • Storing registration ID - Third party app server stores or updates registration ID received from Android app in a database. App server uses this ID for sending message to Android apps.
  • Unregistering C2DM - When Android app does not want to receive C2DM message any more then it can do by calling unregister intent. In response C2DM server will send response which app can use to notify user about un-registration.
Sending message
  • Get Authentication Token - App server can get the authentication token by providing sender ID (Email ID) and password to Google’s client login service. You can get this token via any http tools or command line tools like curl. However, we recommend you to get the authentication token programmatically because over a time this might be changed.
  • App server initiates message sending - App server makes post request to Google’s C2DM server. This request includes auth token and registration ID. This request also contains payload data (key-value pairs) and collapse key (used to collapse group of like messages when device is offline). If C2DM server receives optional parameter delay_while_idle from app server in above request, it will not send message immediately if device is in idle.
  • C2DM server validates credentials - C2DM server will validate credentials received from app server by sending those details to Google authentication service.
  • Queue the message - Once credentials validated C2DM server queues the message.
  • Initiate message delivery - C2DM server initiates message delivery by sending request to C2DM messaging server.
  • Message Delivery - If targeted device (decided based on registration ID) is online then C2DM messaging server delivers message to it. After successful delivery, messaging server removes it from message queue.
Receiving message
  • Receive message - Android device running registered app receives message from C2DM server. Android system extracts key/value pairs from message payload. It then broadcasts the receive intent (com.google.android.c2dm.intent.RECEIVE) with extra parameters.
  • Process message - Application’s broadcast receiver receives intent along with additional information like account name and message. Application processes this data for further usage.
In Part 3 of this blog series we will see error handling, comparison with other push technologies and list of demo applications.

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

1 comment:

  1. Great post!!Thanks for sharing it with us....really needed.Our mission is to understand the value proposition of your business to better target your potential customers, in order to maximize the possibility of converting them into a qualified prospects.agence google