introduction to google cloud messaging in android
TRANSCRIPT
Introduction to Android
Google Cloud Messaging
Amrit SanjeevOrganizer – Bangalore Android User Group
About me
• Android hacker.
• Organizer, Bangalore Android User Group ( www.blrdroid.org )
• Research Engineer at Philips R&D
Bangalore Android User Group ( www.BlrDroid.org)
• Largest open Android developer community in the country and second largest in the world.
• Over 2500 members
• 19 meetups
• 3 hackathons
• Active participation in events like Droidcon, Global Android Developer hackathon etc
What we will be discussing today.
Google Cloud Messaging
1What is GCM?
2Why do we
need GCM?
3How it is
implemented ?
What is GCM?
Google Cloud Messaging
1
GCM definition
GCM stands for Google Cloud Messaging .
It is a free service that allows developers to send data from third party servers to their applications running on android devices
Handles queuing of messages and delivery to the target application running on the target device.
Intended use is not to send huge amount of data to the client device
Things to keep in mind
Application on the android device DOES NOT need to be running to receive messages.
Requires user to set up a Google account on the device.
It is purely a routing component
There is no guaranteed delivery of messages to the device .
Focus of GCM
Ease of use. No sign-up forms. No quotas.
GCM stats are available
through the Android
Developer Console.
Battery efficiency.
Rich set of new APIs
Difference between GCM and C2DM
Simple API Key from Google APIs console page.
Sender id is the project id rather than email address
Supports data in JSON format and plain text
Difference between GCM and C2DM
Multicast message support
Multiple senders
Messages have time to live ( 0 ~ 4 weeks ) eg. Expiring invitations
Payload up to 4kb
2 Why do we need GCM?
The why part
The need to use GCM
Client server architecture is the most common architecture . Most application do all the heavy lifting
and processing at the servers and then use the processed data within the applications .
Scalability & extensibilityMore computing power
The need to use GCM
Background process polling server for information at regular intervals is a bad design Battery performanceSystem resource usage ( memory , CPU )Complicated coding
Application does not need to be running in the background for receiving data messages.
Lower cost and better tracking than SMS.
The need to use GCM
Less coding requiredNot need to worry about queuing of
messagesDiffered delivery
Simpler application flow
The need to use GCM
In a nutshell it makes application design much more simple, improves
battery performance and makes better use of system resources .
3 How it is implemented?
The internals
Major components
Your application server
GCM servers
Mobile device
Typical sequence of events
The application server sends a
message to GCM servers.
Google queues and stores the
message in case the device
is offline.
When the device is
online, Google sends the
message to the device.
On the device, the system
broadcasts the message to the
specified Android. This
wakes the Android
application up.
The Android application
processes the message
option
al
Minimum requirements ( device side )
Android 2.2 +
Android market place should be installed
Emulator with Google APIs
Internet connection
Google account ( device version < 4.0.4 ).
Minimum requirements ( server side )
HTTPS application server that should be Able to communicate with your client.Able to fire off HTTP requests to the
GCM server.Able to handle requests and queue data
as needed. For example, it should be able to perform exponential back off.
Able to store the user device registration id .
Android application
Copy the
required libs
Make changes to the
manifest
Add a broadca
st receiver
Add an Intent
service
Wire it up in your
activity
Alternate Infrastructures
• Alternative to GCM is required under following conditions Limited connectivity to internet servicesSecurity requirement Guaranteed delivery SLAs on deliveryNo quota limits
• DownsidesComplicated designBattery consumption on device is higher.
Thank you In case you have more queries please feel free to contact
[email protected] (+91 97407 44557 )@amsanjeev
BACKUP SLIDES
Add required libs
Install Google Cloud Messaging for
android using SDK manager.
Copy gcm.jar from YOUR_SDK_ROOT/extras/google/gcm-client/dist
to libs in your project folder
Changes to Manifest file
Add <use-sdk … >Declare & use a custom permission Add additional permissions
internet get_accounts wake_lock
Write Intentservice
subclass of com.google.android.gcm.GCMBaseIntentService
must contain a public constructor, Generally named
my_app_package.GCMIntentServiceOverride the following functions with appropriate
logiconRegisteredonUnRegisteredonMessageonErroronRecoverableError
Add boardcast receiver
add a broadcast receiver with the custom permissions and following actions <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action
android:name="com.google.android.c2dm.intent.REGISTRATION" />
Should be defined in the manifest and not programmatically
Finally the activity
CheckDevice ()CheckManifest()getRegistrationId()Register()