Android Mobile Push Gateway Tutorial for Real-Time Apps | PubNub

This page provides a walk-through demonstrating the start to finish process of building a working application utilizing PubNub Mobile Push Gateway. This all-inclusive tutorial includes:

  • Preparing your Android development environment.

  • Preparing your PubNub environment.

  • Building a working application.

  • Testing your application

You can also learn more about the PubNub Mobile Push Gateway for GCM before getting started by following the link.

Overview Contents:

Development Preparation

The following tasks need to be completed to set up Google Cloud Messaging. These steps must be properly completed before beginning work with the PubNub Mobile Push Gateway.

The following steps in this section are specific to provisioning Google Cloud Messaging service. They are provided to make sure you get up and running with PubNub Mobile Push Gateway in as little time as possible.

Building Your PubNub Application

With all of that busywork now out of the way, we’ll focus on getting our application running with PubNub. In this section we will:

  • Enable GCM in our PubNub Account.

  • Review the PubNubExample Application.

  • Test out our new real-time App.

Enable GCM in our PubNub Account

  1. Login to PubNub Admin Portal.

  2. Select the PubNub application you want to enable Push Notifications for.

  3. Scroll down to Push Notifications → Google Cloud Messaging and click on "Add".

  4. It will prompt you for your GCM API key, you can enter your own GCM API Key here or you can enter the following key for testing with the example application:


    If you enter your own GCM API KEY, you would find it in Google Developers Console → YOUR_APP_NAME → Credentials → Public API Access → Server Key Operations → API Key. More info on the API Key here.

  5. Also make note of the application’s associated Subscribe and Publish keys as you will need to specify them in your application (unless you are testing the example application.)

Example Application

  1. If you have not already set up your PubNub Android SDK, the instructions are provided here.

  2. An additional jar will need to be added to your project to leverage GCM functionality. Below is a complete list of jars that should be added to your project:

Sending a Message

The Android SDK ships with an example app called PubnubExample. This is an easy to follow example of how to use every PubNub API call including the new GCM features. To make it easy to get started, we’ve hardcoded some default values in the example app that you can use to test the application. Configuration.

Here are the default values used with the init() method for the example:

 // Use the demo-36 keys
     PUBLISH_KEY = "demo-36";
     SUBSCRIBE_KEY = "demo-36";
     SECRET_KEY = "demo-36";
     // Turn SSL Off (in case we need to debug later)
     SSL = false;
     // Set the Origin to ""
     ORIGIN = "pubsub";
     // This Sender ID corresponds with the example API key you set in the
     // portal. It's the project number from
     // Google Developers Console -> YOUR_APP_NAME
     SENDER_ID = "506053237730";
     // If you are using your own custom Google API Keys, change this to your own
     // value (your Project Number).
Modify AndroidManifest.xml

You’ll need to follow the Google guidelines for assigning the correct application permissions, defining the intent filter, defining the receiver, main activity, etc. More info on that here. We’ve already done this for you in the example app.


In general, you’ll need to add notification icons in the res directory. More info on that here. We’ve already done this for you in the example app.

Add the GCM Broadcast Receiver and the GCM Intent Service

You’ll need to add a GCM broadcast receiver and intent service in the src directory. More info on that here. We’ve already done this for you in the example app.

Running The Application

To test this application:

  1. Deploy PubnubExample to your Android device.

  2. Click the screen, and an options menu will come up.

  3. Scroll to and select "Register on GCM". This will run the gcmRegister() method in the demo app.

  4. Scroll to and select "Add channel to GCM"

  5. Enter a channel, like "demo"

  6. Bring up a web console to the GCM cluster. This link should get you there.

  7. In the PubNub Developers Console you can send the following JSON to the channel you registered with GCM:

 {"pn_gcm": {
        "data" : {
            "GCMSays" : "hi" }

You should now have received this data as a notification in your Android app.

To send a sample via the file, from the java/java/jars directory, you can issue the command:

java -jar PubnubPushTest.jar --gcm --channel demo --origin pubsub

To send the same in Java, you can do the following:

   // Create GCM Message
     PnGcmMessage gcmMessage = new PnGcmMessage();
     // Create the payload.
     JSONObject jso = new JSONObject();
     try {
         jso.put("GCMSays", "hi");
         jso.put("moreInfoIfYouWant", "WhereAreTheKidsIDontKnow");
     } catch (JSONException e) {
     // By setting the payload on the gcmMessage object, this effectively populates
     // the payload as a value of the required GCM "data" key.
     Callback callback = new Callback() {
         public void successCallback(String channel, Object response) {
         public void errorCallback(String channel, PubnubError error) {
     // sender takes channel, PN Instance, and Callback as parameters.
     PubnubSender sender = new PubnubSender(channel, pubnub, callback);
     // Reset the object so we don't send previously set data.
     PnMessage message = null;
     message = new PnMessage(sender, gcmMessage);
     try {
     } catch (PubnubSenderMissingException e) {
         System.out.println("Set Sender");
Receiving a Message

The PNMessage object allows you to send unique GCM, APNS, and Native pubnub traffic to separate devices in a single publish operation. is an example of Publishing with the new PNMessage object.