13
Serious Android Developer Google Cloud Messaging www.seriousandroiddeveloper.in

Google Cloud Messaging, Android Simple Tutorial for beginner

Embed Size (px)

DESCRIPTION

Join our facebook Group for more examples : http://www.facebook.com/groups/c2dmdeveloper/http://www.seriousandroiddeveloper.in/Download ppt :http://ultraimager.webs.com/Seriousandroiddeveloper/GCM_tutorial.pptxDownload client app:http://ultraimager.webs.com/Seriousandroiddeveloper/C2DM%20example%20and%20sample%20source.rarDownload server code: http://ultraimager.webs.com/Seriousandroiddeveloper/GCMserver.zipUnable to Create Third Party server?, want to test this technology click following http://ultraneo.comuv.com/GCMserverforSerious/ Happy Programming

Citation preview

Page 1: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Google Cloud Messagingwww.seriousandroiddeveloper.in

Page 2: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Overview•Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android applications on Android devices.

•Message containing up to 4kb of payload data.

•The GCM service handles all aspects of queuing of messages and delivery to the target Android application running on the target device.

•It doesn’t necessary that your application in running state 24X7.

Page 3: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Requirement:1. Application Server.

2. Google Account.

3. Android Phone/ emulator(Google API).

Page 4: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Terms:1. Registration ID- An ID issued by the GCM servers to the Android application

that allows it to receive messages. Once the Android application has the registration ID, it sends it to the 3rd-party application server, which uses it to identify each device that has registered to receive messages for a given Android application. In other words, a registration ID is tied to a particular Android application running on a particular device.

2. Sender Auth Token- An API key that is saved on the 3rd-party application server that gives the application server authorized access to Google services. The API key is included in the header of POST requests that send messages.

3. Sender ID- A project ID you acquire from the API console The sender ID is used in the registration process to identify an Android application that is permitted to send messages to the device.

4. Pay Load – your data.

Page 5: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

GCM Server

How it works

Our Server

1.Client will send activation request, via our application 2.Client will receive

response with unique Registration ID.

3.Client need to send this Registration ID, to our app server. This registration ID is used to send data to the phone.

4.When ever server have any message for a particular device , it send the request to the GCM server using Registration ID.

5. GCM server push the data with GCM technology

Page 6: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Get access to Google ServerYou need to create a Google API project open the following link https://code.google.com/apis/console/ and activate GCM API service to your project

Note down:1. Project Number(SENDER ID)2. API key(Sender Auth Token)

Page 7: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Writing code at client side / android appPermission:<manifest package=“in.seriousandroiddeveloper.gcm" ...> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission android:name=“in.seriousandroiddeveloper.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="in.seriousandroiddeveloper.gcm.permission.C2D_MESSAGE" />

<receiver android:name=".SeriousBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="in.seriousandroiddeveloper.gcm" /> </intent-filter></receiver>

Page 8: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Writing code at client side / android appRegistration :Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");registrationIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));registrationIntent.putExtra("sender", SenderID);startService(registrationIntent);

Deactivation:Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");unregIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));startService(unregIntent);

Page 9: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Writing code at client side / android appCreate a Broadcaster Receiver class: SeriousBroadcastReceiver public class SeriousBroadcastReceiver extends BroadcastReceiver {public void onReceive(Context context, Intent intent) {try { String action = intent.getAction(); if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {

String registrationId = intent.getStringExtra("registration_id");String error = intent.getStringExtra("error");String unregistered = intent.getStringExtra("unregistered");

} else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {

String data1 = intent.getStringExtra(“data1");String data2 = intent.getStringExtra(“data2");

} } finally { }}}

Page 10: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Writing code at client side / android appProcessing (action.equals("com.google.android.c2dm.intent.REGISTRATION")) String error = intent.getStringExtra("error")If (error.equals.(“~~~~~~~~~”))SERVICE_NOT_AVAILABLE The device can't read the response, or there was a 500/503 from the server that can be retried later. The Android application should use exponential back-off and retry.•ACCOUNT_MISSING-There is no Google account on the phone. The Android application should ask the user to open the account manager and add a Google account. Fix on the device side.•AUTHENTICATION_FAILED-Bad Google Account password. The Android application should ask the user to enter his/her Google Account password, and let the user retry manually later. Fix on the device side.•INVALID_SENDER-The sender account is not recognized. This must be fixed on the Android application side. The developer must fix the application to provide the right sender extra in thecom.google.android.c2dm.intent.REGISTER intent.PHONE_REGISTRATION_ERROR-Incorrect phone registration with Google. This phone doesn't currently supportGCM.INVALID_PARAMETERS-The request sent by the phone does not contain the expected parameters. This phone doesn't currently support GCM.

Page 11: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Writing code at Server sideServer implementation is via HTTP request : To send a message, the application server issues a POST request to https://android.googleapis.com/gcm/send.

A message request is made of 2 parts: HTTP header and HTTP body.

The HTTP header1.Authorization: key=YOUR_API_KEY2. Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text.

HTTP Body:1. registration_id2. data.<key>

Page 12: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer

Join usSerious Android Developer

f Group

www.seriousandroiddeveloper.in

Page 13: Google Cloud Messaging, Android Simple Tutorial for beginner

Serious Android Developer