Cloud to Device Messaging can be used so your app receives messages without polling.
This saves battery power, it also minimizes data traffic and server load.
When a server app has a message for a specific client it connects to Google's server which then passes the message to the client.
The maximum size of the message is 1 kilobyte so the message should only contain information about where the client can download the data referred to by the message.
Client RegistrationTo receive messages, a client app needs to first obtain a registration ID by starting a service with the following kind of intent:
Action=
Extras=
com.google.android.c2dm.intent.REGISTER
Once the service has obtained the registration id it will call back to a Receiver which has, in its intent filter, specified
com.google.android.c2dm.intent.REGISTRATION as the action and the apps package name as the category.This receiver requires the
com.google.android.c2dm.permission.SEND permission.This receiver can be specified in the manifest.xml or dynamically from your app's code using
registerReceiver The registration ID will be passed into the receiver in the
registration_id extra of the intent that started the receiver.Send this ID to your server and store it there.
In order for an app to register to c2dm (and later to receive messages), the manifest must request these permissions:
To unregister use the same intent but specify the
com.google.android.c2dm.intent.UNREGISTER action.Server RegistrationSign up here.
Before you can send messages, you need to authorize your server with Google's server as described in Authentication and Authorization for Google APIs
This will result in an auth token which is required when sending the actual messages.
To get the auth token manually use this command
curl https://www.google.com/accounts/ClientLogin -d Email= theServerAccountYouSignedUpFor -d Passwd=yourPassword -d service=ac2dm Which should return a key named
Auth Whenever your server wants to send a message, this token should be included in the request.
Sending MessagesTo send a message, your server should make an HTTP post request tohttps://android.apis.google.com/c2dm/send with these parameters:
The request should also include this header:
Authorization: GoogleLogin auth= TheAuthTokenYouGotWhenRegistering To send a message manually use this command curl --header "Authorization: GoogleLogin auth=YourToken" "https://android.apis.google.com/c2dm/send" -d registration_id=YourRegistrationId -d "data.message=YourMessage" -d collapse_key=YourCollapseKey -k Receiving MessagesFor your app to receive the message, you need to define another Receiver and specify the
com.google.android.c2dm.permission.SEND permission.Its intent filter should have the action
com.google.android.c2dm.intent.RECEIVE and the category set to your apps package name.It should specify the
com.google.android.c2dm.permission.SEND permission as well.The intent which started the receiver's onReceive method will have a number of extras with the names which the server specified in the
data.<key> parameters. The values of these extras is your message which can then be handled any way your app wants.More information:
Google Projects for Android: C2DM |
Development > Tutorials >