Google Cloud Messaging: Overview
Google Cloud Messaging (GCM) is a free service that enables developers to send messages between servers and client apps. This includes downstream messages from servers to client apps, and upstream messages from client apps to servers.
For example, a lightweight downstream message could inform a client app that there is new data to be fetched from the server, as in the case of a "new email" notification. For use cases such as instant messaging, a GCM message can transfer up to 4kb of payload to the client app. The GCM service handles all aspects of queueing of messages and delivery to and from the target client app.
Architectural Overview
A GCM implementation includes a Google connection server, an app server in your environment that interacts with the connection server via HTTP or XMPP protocol, and a client app.
Here's how these components interact:
Key Concepts
Below summarizes the key terms and concepts involved in GCM. It is divided into these categories:
GCM components and credentials.
Components
Credentials
Sender ID
A unique numerical value created when you configure your API project. The sender ID is used in the registration process to identify an app server that is permitted to send messages to the client app.
Server key
A key saved on the app server that gives the app server authorized access to Google services. In HTTP, the server key is included in the header of POST requests that send messages. In XMPP, the server key is used in the SASL PLAIN authentication request as a password to authenticate the connection. Do not include the server key anywhere in your client code. You obtain the server key when you create your API project.
Application ID
The client app that is registering to receive messages. How this is implemented is platform-dependent:
Registration Token
An ID issued by the GCM connection servers to the client app that allows it to receive messages. Note that registration tokens must be kept secret.
Lifecycle Flow
Official Documentation Reference can be found here.
Implement onMessageReceived
that will catch the notification sent from GCM server.
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
// Handle received message here.
}
To receive the notification, implement application:didReceiveRemoteNotification:fetchCompletionHandler:
(or application:didReceiveRemoteNotification:
for iOS < 8.0), and call GCMService:appDidReceiveMessage:message
to acknowledge the reception of the message to GCM.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Notification received: %@", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// ...
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(@"Notification received: %@", userInfo);
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// ...
}
Send a message using GCM HTTP connection server protocol:
https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a GCM Topic Message!",
}
}
To do this in Postman, you simply have to set the following (some details are as what is mentioned above):
POST
Screenshots:
Notice that the request was a success with the message_id
in the response.
PS: I'm keeping the sample Server Key visible so that others can still try it out even if they haven't created a Project yet. BUT, note that the Server Key must be always kept secret.