Gcm presentation

23
GCM Google Cloud Messaging for Android Niraj Singh

Transcript of Gcm presentation

Page 1: Gcm presentation

GCMGoogle Cloud Messaging for Android

Niraj Singh

Page 2: Gcm presentation

GCM: Introduction• GCM (Google Cloud Messaging) is a free service that helps developers

send data from servers to their Android applications on Android devices.

• Using GCM we can push lightweight message to applications telling that there is new data to be fetched from the server or

a message containing up to 4kb of payload data (e g: instant messaging apps).

• This can eliminate continuous query to server for updates using background services

Page 3: Gcm presentation

GCM: Characteristics• Allows 3rd-party application servers to send messages to their Android

applications.

• GCM makes no guarantees about delivery or the order of messages.

• Application on an Android device doesn't need to be running to receive messages.

• Requires devices running Android 2.2 or higher (with Google Play Store application installed), or an emulator running Android 2.2 with Google APIs.

• Uses an existing connection for Google services. For pre-3.0 devices, this requires users to set up their Google account on their mobile

devices. A Google account is not a requirement on devices running Android 4.0.4 or higher.

Page 4: Gcm presentation

GCM: Architectural Overview Key Terms: key terms and concepts involved in GCM are divided

into these categories.

• Components -The physical entities that playa role in GCM.

• Credentials -The IDs and tokens that are used in different stages of GCM to ensure that all parties have been authenticated, and that the message is going to the correct place.

Page 5: Gcm presentation

GCM Architecture: Components Components

• Mobile Device -The device that is running an Android application that uses GCM. This must be a 2.2 Android device that has Google Play Store installed, and it must have at least one logged in Google account if the device is running a version lower than Android 4.0.4. Alternatively, for testing you can use an emulator running Android 2.2 with Google APIs.

• 3rd-party Application Server - An application server that developers set up as part of implementing GCM in their applications. The 3rd-party application server sends data to an Android application on the device via the GCM server.

• GCM Servers - The Google servers involved in taking messages from the 3rd-party application server and sending them to the device.

Page 6: Gcm presentation

GCM Architecture: Components

Page 7: Gcm presentation

GCM Architecture: Credentials Credentials• Sender ID - A project ID you acquire from the API console to identify an

Android application that is permitted to send messages to the device.• Application ID - The Android application that is registering to receive

messages.• Registration ID - An ID issued by the GCM servers to the Android

application that allows it to receive messages.• Google User Account - For GCM to work, the mobile device must include

at least one Google account if the device is running a version lower than Android 4.0.4.

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

Page 8: Gcm presentation

GCM: Implementation

• Enabling GCM - An Android application running on a mobile device registers to receive messages.

• Sending a Message - A 3rd-party application server sends messages to the device. (we'll do it with php)

• Receiving a Message - An Android application receives a message from a GCM server.

Page 9: Gcm presentation

GCM: Implementation Steps (1)• Goto https://code.google.com/apis/console• Create a project - Project_Name• Browser link has changed as https://code.google.com/apis/console/? pli=1#project:351708780651:services• it contains project id (94384365614 in this example)• We required that in our application as sender id.

Page 10: Gcm presentation

GCM: Implementation Steps (2)• In the main Google APIs Console page, select Services. Turn the Google Cloud Messaging toggle to ON. In

the Terms of Service page, accept the terms.

Page 11: Gcm presentation

GCM: Implementation Steps (3)• In the main Google APIs Console page, select API Access. Now you can see

there is API key if you use that key your application can receive messages fromany server & if you want to restrict servers you can generate new server keyusing button there as “Create new server key…”.

Page 12: Gcm presentation

GCM: Implementation Steps (3.1)Now we have Sender ID and API KEY

• Sender ID - 351708780651

• API KEY - AIzaSyBFhGpJswkvMxMTElfAQeMUskG13ii7s1Q

Page 13: Gcm presentation

GCM: Implementation Steps (4)

• Create new Android Project

• Copy the gcm.jar file from - SDK~PATH/extras/google/gcm/gcm-client/dist directory to your application classpath. As external jar.

Page 14: Gcm presentation

GCM: Implementation Steps (6) Permissions in Manifest

• INTERNET – To make your app use internet services. <uses-permission android:name="android.permission.INTERNET" />

• ACCESS_NETWORK_STATE – To access network state (used to detect internet status)

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

• GET_ACCOUNTS – Required as GCM needs google account <uses-permission android:name="android.permission.GET_ACCOUNTS" />

Page 15: Gcm presentation

GCM: Implementation Steps (6)

Permissions in Manifest

• WAKE_LOCK – Needed if your app need to wake your device when it sleeps <uses-permission android:name="android.permission.WAKE_LOCK" />

• VIBRATE – Needed if your support vibration when receiving notification Also add some broadcast receivers as mentioned below.

<uses-permission android:name="android.permission.VIBRATE" />

Page 16: Gcm presentation

GCM: Implementation Steps (7)• Add GCM Receiver

BroadcastReceiver that will receive intent from GCM Services and handle themTo the custom Intent Services.

<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <!-- Receives the actual messages. --> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <!-- Receives the registration id. --> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.androidexample.gcm" /> </intent-filter> </receiver>

Page 17: Gcm presentation

GCM: Implementation Steps (9)• Add GCMIntentService extending GCMBaseIntentService and add the service in

Manifest

<service android:name="com.androidexample.gcm.GCMIntentService" />

public class GCMIntentService extends GCMBaseIntentService {

/** * Method called on device registered **/ @Override protected void onRegistered(Context context, String registrationId) {

/** * Method called on device unregistred * */ @Override protected void onUnregistered(Context context, String registrationId) {

Page 18: Gcm presentation

GCM: Implementation Steps (9)• Add GCMIntentService extending GCMBaseIntentService and add the service in

Manifest

/**

* Method called on Receiving a new message from GCM server * */ @Override protected void onMessage(Context context, Intent intent) {

/** * Method called on receiving a deleted message * */ @Override protected void onDeletedMessages(Context context, int total) {

Page 19: Gcm presentation

GCM: Implementation Steps (10)

• Register: // Make sure the device has the proper dependencies. GCMRegistrar.checkDevice(this);

// Make sure the manifest permissions was properly set GCMRegistrar.checkManifest(this);

// Get GCM registration idfinal String regId = GCMRegistrar.getRegistrationId(this);

// Check if regid already presentsif (regId.equals("")) {

// Register with GCMGCMRegistrar.register(this, Config.GOOGLE_SENDER_ID);} else {

Page 20: Gcm presentation

GCM: Implementation Steps (10) Register:

// Device is already registered on GCM Serverif (GCMRegistrar.isRegisteredOnServer(this)) {

// Skips registration.Toast.makeText(getApplicationContext(), "Already registered with GCM Server", Toast.LENGTH_LONG).show();

}

Page 21: Gcm presentation

GCM: Implementation Steps (10)

• Unregister:// Unregister Broadcast Receiver

unregisterReceiver(mHandleMessageReceiver);

Page 22: Gcm presentation

GCM: Implementation Steps (11)• Receive Message

/** * Method called on Receiving a new message from GCM server * */ @Override protected void onMessage(Context context, Intent intent) { if(aController == null) aController = (Controller) getApplicationContext(); Log.i(TAG, "Received message"); String message = intent.getExtras().getString("price"); aController.displayMessageOnScreen(context, message); // notifies user generateNotification(context, message); }

Page 23: Gcm presentation

GCM: Implementation Steps (12)• Send Message from Server

//Sending Push Notification function send_push_notification($registatoin_ids, $message) {

// Set POST variables $url = 'https://android.googleapis.com/gcm/send';

$fields = array( 'registration_ids' => $registatoin_ids, 'data' => $message, );

$headers = array( 'Authorization: key=' . GOOGLE_API_KEY, 'Content-Type: application/json' );