Google Cloud Messaging Report
Google Cloud Messaging Report
ON
Submitted By:
VINEET GARG
2K13/CO/142
CERTIFICATE
This is to certify that this project report entitled Google Cloud Messaging
submitted by Vineet Garg (2K13/CO/142) in partial fulfilment for the requirements
for the award of Bachelor of Technology Degree in Computer Engineering (COE) at
Delhi Technological University is an authentic work carried out by the student under
my supervision and guidance.
To the best of my knowledge, the matter embodied in the thesis has not been
submitted to any other university or institute for the award of any degree or diploma.
ACKNOWLEDGEMENT
I would like to express my greatest gratitude to the people who have helped and
supported me throughout the project.
Firstly, I express my heartiest gratitude towards the authorities who gave me a chance
to explore the intricacies of various aspects of Google Cloud Messaging. I would
also sincerely thank my esteemed mentor, Ms. Indu Singh, who lent a huge helping
hand in the process of making this project with her valuable guidance and blessings.
I would also not forget to thank the department authorities, who played an
indispensable role in the process. In the end, I would thank my friends and family for
their extended support throughout the project.
Vineet Garg
ABSTRACT
Google Cloud Messaging for Android (GCM) is a service that helps developers to
send data from servers to their Android applications on Android devices. The service
was unveiled on June 27, 2012, at Google I/O 2012 held at the Moscone Center in
San Francisco . The GCM service handles all aspects of queuing of messages and
delivery to the target Android application running on the target device. It is
completely free whatever your messaging needs are and it can be used in some
applications like smart notification systems.
This report gives an overview about the Google GCM service and about its working
and usage.
INDEX
S.No
Topic
Page no.
Introduction
Key Concepts
Architectural Overview
Lifecycle Flow
10
12
Properties of Payload
18
Possible Problems
19
Applications
20
Conclusion
21
10
References
22
INTRODUCTION
Google Cloud Messaging (GCM) is a service that enables developers to send data
from servers to both Android applications or Chrome apps and extensions.
The service provides a simple, lightweight mechanism that servers can use to tell
mobile applications to contact the server directly, to fetch updated application or user
data. The service handles all aspects of queueing of messages and delivery to the
target application running on the target device.
The free service has the ability to send a lightweight message informing the Android
application of new data to be fetched from the server. Larger messages can be sent
with up to 4 KB of payload data. Each notification message size is limited to 1024
bytes, and Google limits the number of messages a sender sends in aggregate, and the
number of messages a sender sends to a specific device.
Applications on an Android device dont need to be running to receive messages. The
system will wake up the application via a mechanism called Intent Broadcast when
the message arrives, as long as the application is set up with the proper broadcast
receiver and permissions. GCM does not provide any built-in user interface or other
handling for message data. Instead, it simply passes raw message data received
straight to the application, which has full control of how to handle it. For example,
the application might post a notification, display a custom user interface, or silently
sync data.
KEY CONCEPTS
This table summarizes the key terms and concepts involved in GCM. It is divided
into these categories:
Components The entities that play a primary 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.
Table 1. GCM components and credentials.
Components
Client App
3rd-party
Application
Server
An application server that you write as part of implementing GCM. The 3rdparty application server sends data to an Android application on the device via
the GCM connection server.
GCM
Connection
Servers
Sender ID
A project number you acquire from the API console, as described in Getting
Started. The sender ID is used in the registration process to identify a 3rd-party
application server that is permitted to send messages to the device.
Application ID
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
An API key that is saved on the 3rd-party application server that gives the
Token
ARCHITECTURAL OVERVIEW
A GCM implementation includes a Google-provided connection server, a 3rd-party
app server that interacts with the connection server, and a GCM-enabled client app
running on an Android device:
LIFECYCLE FLOW
(a)Enable GCM. An Android application running on a mobile device registers to
receive messages.
(b)Send a message. A 3rd-party application server sends messages to the device.
(c)Receive a message. An Android application receives a message from a GCM
server.
11
number.
2)
Enable GCM service on that project from API's and Auth section.
3)
Create new Server Key from APIs & auth > Credentials.
4)
a) Set up Setup Google Play Services SDK for the Android project.
12
vi) Pass recived message to Intent Service for payload parsing and
pertorming necessary action.
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm =
GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the
intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
Bundle
/*
* Filter messages based on message type. Since it is
likely that GCM
* will be extended in the future with new message
types, just ignore
* any message types you're not interested in, or that
you don't
* recognize.
*/
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " +
extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some
work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " +
SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " +
SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification("Received: " +
extras.toString());
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the
WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to
do with
// a GCM message.
16
5)
17
PROPERTIES OF PAYLOAD
1)
2)
3)
A message sent to GCM server has a lifetime of 4 weeks, that is it will reside
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
"registration_id" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"data" : {
"Team" : "18",
"Score" : "3",
"Player" : "Varela",
},
}
18
POSSIBLE PROBLEMS
There may be a case that user has uninstalled the applications. Thus the
sender id we are referring to does not exit. In that case GCM server will
response back to application server that the following Id does not exist.
In that its entry from database of application server shoulb be deleted
Although Sender Id is constant for an install of application, it changes if
there is an update to the application, in that care must be taken to reregister device to GCM and also change it's Sender Id in database of
application server.
The payload limit is only 4kb, so we can not send data greater than 4kB
like genarally media files etc.
19
APPLICATIONS
GCM has the following applications:
1)
Telegram etc.
2)
20
CONCLUSION
1)
2)
in devices.
3)
4)
5)
The message sent to GCM server is a JSON encoded string with keys
The response sent back to 3rd party server from GCM server is also a
8)
9)
delievered
10) Instant messangers on Android use this service to send their
messages.
11) Other third party apps uses this services to push new notifications for
their apps.
21
REFERENCES
1)
http://developer.android.com/google/gcm/gcm.html
2)
http://developer.android.com/google/gcm/gs.html
3)
http://developer.android.com/google/gcm/client.html
4)
http://developer.android.com/google/gcm/server.html
5)
http://developer.android.com/google/gcm/notifications.html
6)
http://developer.android.com/google/gcm/adv.html
7)
http://en.wikipedia.org/wiki/Google_Cloud_Messaging
22