introduction to google cloud messaging in android

29
Introduction to Android Google Cloud Messaging Amrit Sanjeev Organizer – Bangalore Android User Group

Upload: ria-rui-society

Post on 10-May-2015

9.047 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to google cloud messaging in android

Introduction to Android

Google Cloud Messaging

Amrit SanjeevOrganizer – Bangalore Android User Group

Page 2: Introduction to google cloud messaging in android

About me

• Android hacker.

• Organizer, Bangalore Android User Group ( www.blrdroid.org )

• Research Engineer at Philips R&D

Page 3: Introduction to google cloud messaging in android

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

Page 4: Introduction to google cloud messaging in android

What we will be discussing today.

Google Cloud Messaging

1What is GCM?

2Why do we

need GCM?

3How it is

implemented ?

Page 5: Introduction to google cloud messaging in android

What is GCM?

Google Cloud Messaging

1

Page 6: Introduction to google cloud messaging in android

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

Page 7: Introduction to google cloud messaging in android

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 .

Page 8: Introduction to google cloud messaging in android

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

Page 9: Introduction to google cloud messaging in android

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

Page 10: Introduction to google cloud messaging in android

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

Page 11: Introduction to google cloud messaging in android

2 Why do we need GCM?

The why part

Page 12: Introduction to google cloud messaging in android

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

Page 13: Introduction to google cloud messaging in android

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.

Page 14: Introduction to google cloud messaging in android

The need to use GCM

Less coding requiredNot need to worry about queuing of

messagesDiffered delivery

Simpler application flow

Page 15: Introduction to google cloud messaging in android

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 .

Page 16: Introduction to google cloud messaging in android

3 How it is implemented?

The internals

Page 17: Introduction to google cloud messaging in android

Major components

Your application server

GCM servers

Mobile device

Page 18: Introduction to google cloud messaging in android

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

Page 19: Introduction to google cloud messaging in android

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 ).

Page 20: Introduction to google cloud messaging in android

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 .

Page 21: Introduction to google cloud messaging in android

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

Page 22: Introduction to google cloud messaging in android

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.

Page 23: Introduction to google cloud messaging in android

Thank you In case you have more queries please feel free to contact

[email protected] (+91 97407 44557 )@amsanjeev

Page 24: Introduction to google cloud messaging in android

BACKUP SLIDES

Page 25: Introduction to google cloud messaging in android

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

Page 26: Introduction to google cloud messaging in android

Changes to Manifest file

Add <use-sdk … >Declare & use a custom permission Add additional permissions

internet get_accounts wake_lock

Page 27: Introduction to google cloud messaging in android

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

Page 28: Introduction to google cloud messaging in android

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

Page 29: Introduction to google cloud messaging in android

Finally the activity

CheckDevice ()CheckManifest()getRegistrationId()Register()