Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

This branch is 1573 commits behind pubnub:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

..
Failed to load latest commit information.
bin
gen/com/fbt
res
src/com/fbt
.classpath
.project
AndroidManifest.xml
README
default.properties
proguard.cfg

README

## ----------------------------------------------------
##
## VIDEO
## http://www.youtube.com/watch?v=pkxUYYhwb04
##
## ----------------------------------------------------

## ----------------------------------------------------
##
## YOU MUST HAVE A PUBNUB ACCOUNT TO USE THE API.
## http://www.pubnub.com/account
##
## ----------------------------------------------------

## ----------------------------------------------------
## PubNub 3.0 Real-time Cloud Push API - ANDROID
## ----------------------------------------------------
##
## www.pubnub.com - PubNub Real-time Push Service in the Cloud. 
## http://www.pubnub.com/tutorial/java-push-api
##
## 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
===============================================================================

-------------------------------------------------------------------------------
Java: (Init)
-------------------------------------------------------------------------------

    Pubnub pubnub = new Pubnub(
        "demo",  // PUBLISH_KEY
        "demo",  // SUBSCRIBE_KEY
        "",      // SECRET_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
    JSONArray info = pubnub.publish(
        "hello_world", // Channel Name
        message        // JSON Message
    );

    // Print Response from PubNub JSONP REST Service
    System.out.println(info);


-------------------------------------------------------------------------------
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
            System.out.println(message);

            // Continue Listening?
            return true;
        }
    }

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

    // Listen for Messages (Subscribe)
    pubnub.subscribe(
        "hello_world",   // Channel Name
        message_receiver // Receiver Callback Class
    );


-------------------------------------------------------------------------------
Java: (History)
-------------------------------------------------------------------------------

    // Get History
    JSONArray response = pubnub.history(
        "hello_world", // Channel Name
        1              // Limit
    );

    // Print Response from PubNub JSONP REST Service
    System.out.println(response);
    System.out.println(response.optJSONObject(0).optString("some_key"));

Something went wrong with that request. Please try again.