Find file History
Pull request Compare This branch is 1 commit ahead, 1239 commits behind pubnub:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



# Android Updates Provided By

Garett Rogers - [@GarettRogers](


## PubNub 3.0 Real-time Cloud Push API - ANDROID - PubNub Real-time Push Service in the Cloud.

PubNub is a Massively Scalable Real-time Service for Web and Mobile Games.
This is a cloud-based service for broadcasting Real-time messages
to thousands of web and mobile clients simultaneously.

## PubNub Java Client API Boiler Plate

This is a full android sample app with the ability to Subscribe
and UnSubscribe from PubNub channels.
This will lead you within the resources allotted for your contract using the
PubNub Cloud.  By UnSubscribing from channels, you will save resources and
ultimately save billing costs and save on prices.  Note that the example
included has only shown you how to properly connect/disconnect users
from a subscribed channel.  You must review the included example app.
I've also included a screenshot with the navigation pointing to the important
reference file needed to learn how to use the PubNub UnSubscribe ability.

The Attached Android App provides you Two new Abilities/Examples:
Subscribe + UnSubscribe to a PubNub Channel.
Provides a superior alternative to C2DM for broadcasting messages to entire user base.

### How to Get Stared with Sub/UnSub Methods:

- Just click subscribe or unsubscribe.  The sample included provides a sample UI which interacts with the new Sub/UnSub methodology.
- While subscribed, sending any message to "androidsample" will show a popup in the app saying that it received a message.
- When unsubscribed, nothing will happen when you send that message, because the connection goes away.

- C2DM Alternative:
- First launch of the app will start the service.
- Phone boot starts the service as well.
- Send message to "c2dmalt" on with this format:

    "title":"Android PubNub", 
    "text" : "This is a push to all users! woot!", 
    "url" : ""

- You will see a push notification in the Android Notification Bar with your message!
- This is MUCH preferred to than the C2DM slow network that Google provides you. C2DM is hard to implement, it's imposes artificial limits you reach very quickly.  C2DM is not recommended to use it as a "broadcast" mechanism according to Google.  C2DM is Slow.  Google forces you to send a single message at a time (1 http connection per message).

- The method of unsubscribing is harsh yet swift, all resources are cleared associated to the connections.
- The C2DM alternative will work as long as they have an internet connection or can re-connect before the queue has been freed.  PubNub is reliable this way.  In the case where the user may be between network signals, or their phone is off for extended period of time.  They will not receive the message.

## Java: (Init)

    Pubnub pubnub = new Pubnub(
        "demo",  // PUBLISH_KEY
        "demo",  // SUBSCRIBE_KEY
        "",      // SECRET_KEY
        "",      // CIPHER_KEY
        false    // SSL_ON?

## Java: (Publish)

    // Create JSON Message
    JSONObject message = new JSONObject();
    try { message.put( "some_key", "Hello World!" ); }
    catch (org.json.JSONException jsonError) {}

    // Publish Message
    // Create HashMap parameter
	HashMap<String, Object> args = new HashMap<String, Object>(2);
	args.put("channel", "hello_world");		// Channel Name
	args.put("message", message);			// JSON Message
    // Publish Message
    JSONArray info = pubnub.publish( args );

    // Print Response from PubNub JSONP REST Service

## Java: (Subscribe) **NOTE** Put inside a Thread!

    // Callback Interface when a Message is Received
    class Receiver implements Callback {
        public boolean execute(JSONObject message) {

            // Print Received Message

            // Continue Listening?
            return true;

    // Create a new Message Receiver
    Receiver message_receiver = new Receiver();

    // Create HashMap parameter
    HashMap<String, Object> args = new HashMap<String, Object>(2);
	args.put("channel", "hello_world");			// Channel Name
	args.put("callback", message_receiver);		// Receiver Callback Class
    // Listen for Messages (Subscribe)
    pubnub.subscribe( args );

## Java: (History)

    // Create HashMap parameter
    HashMap<String, Object> args = new HashMap<String, Object>(2);
	args.put("channel", "hello_world");		// Channel Name
	args.put("limit", 1);					// Limit
    // Get History
    JSONArray response = pubnub.history( args );

    // Print Response from PubNub JSONP REST Service