SelectPubNub BlackBerry SDK 3.7.11




  1. Open the Eclipse (Blackberry Java Plugin)
  2. Goto File -> New -> Blackberry Project
  3. Enter a project name ( for this example, we'll use PubnubExample ) and click Next
  4. Click "Link additional Source"
  5. Browse to and select java/blackberry/examples/PubnubExample/src and enter Folder name as src1
  6. Click "Finish"
  7. Select the Libraries tab
  8. Click "Add External JARs"
  9. Select bouncycastle.jar and Pubnub-Microedition-3.7.11.jar from libs
  10. Go to the "Order and Export" tab and check the boxes against bouncycastle.jar and Pubnub-Microedition-3.7.11.jar.
  11. Click "Finish"
  12. Create a Run Configuration by clicking Run -> Run Configurations from the menu
  13. Select Blackberry Simulator, right-click, and select New, you will now have access to some new tabs.
  14. Click the "Projects" tab
  15. Check the box against PubnubExample
  16. Click the "Simulator" tab, and confirm the Launch Mobile Data System Connection Service (MDS-CS) with simulator checkbox is selected
  17. Change the configuration name to PubnubExample
  18. Click "Apply"
  19. Right click PubnubExample in package explorer.
  20. Select Run As -> Blackberry Simulator. This will start MDS on command prompt and Blackberry Simulator
     
    NOTE: MDS may need to be manually started by running run.bat located at Eclipse\plugins\net.rim.ejde.componentpack7.1.0_7.1.0.10\components\MDS
  21. The MDS console will display debug information as the simulator phone boots.
  22. Once the simulator has booted, scroll to and select PubnubExample (it will have the default “App Window” icon)
  23. Run it, and select the menu button to see it run various PubNub for Blackberry API methods.
 Pubnub pubnub = new Pubnub("demo", "demo");
 
 try {
   pubnub.subscribe("my_channel", new Callback() {
       @Override
       public void connectCallback(String channel, Object message) {
           pubnub.publish("my_channel", "Hello from the PubNub Java SDK", new Callback() {});
       }

       @Override
       public void disconnectCallback(String channel, Object message) {
           System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel
                      + " : " + message.getClass() + " : "
                      + message.toString());
       }
 
       public void reconnectCallback(String channel, Object message) {
           System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel
                      + " : " + message.getClass() + " : "
                      + message.toString());
       }
 
       @Override
       public void successCallback(String channel, Object message) {
           System.out.println("SUBSCRIBE : " + channel + " : "
                      + message.getClass() + " : " + message.toString());
       }
 
       @Override
       public void errorCallback(String channel, PubnubError error) {
           System.out.println("SUBSCRIBE : ERROR on channel " + channel
                      + " : " + error.toString());
       }
     }
   );
 } catch (PubnubException e) {
   System.out.println(e.toString());
 }
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance, and the secret_key if you wish to perform PAM administrative operations from this BlackBerry instance.
 

For security reasons you should only include the secret-key on a highly secured server. The secret-key is only required for granting rights using our Access Manager.

When you init with secret_key, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

Pubnub pubnub = new Pubnub("demo", "demo");
 
If you no longer need to use the Pubnub instance, do not forget to explicitly stop it's running threads using shutdown() method.
Call time() to verify the client connectivity to the origin:
 Callback callback = new Callback() {
   public void successCallback(String channel, Object response) {
     System.out.println(response.toString());
   }
   public void errorCallback(String channel, PubnubError error) {
   System.out.println(error.toString());
   }
 };
 pubnub.time(callback);
Subscribe (listen on) a channel (it's async!):
pubnub.subscribe("my_channel", new Callback() {

      @Override
      public void connectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : CONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void disconnectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      public void reconnectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void successCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : " + channel + " : "
                     + message.getClass() + " : " + message.toString());
      }

      @Override
      public void errorCallback(String channel, PubnubError error) {
          System.out.println("SUBSCRIBE : ERROR on channel " + channel
                     + " : " + error.toString());
      }
    }
  );
Publish a message to a channel:
Callback callback = new Callback() {
  public void successCallback(String channel, Object response) {
    System.out.println(response.toString());
  }
  public void errorCallback(String channel, PubnubError error) {
	System.out.println(error.toString());
  }
};
pubnub.publish("my_channel", "Hello from the PubNub Java SDK!" , callback);
Get occupancy of who's here now on the channel by UUID:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
Callback callback = new Callback() {
    public void successCallback(String channel, Object response) {
        System.out.println(response.toString());
    }
    public void errorCallback(String channel, PubnubError error) {
        System.out.println(error.toString());
    }
};
pubnub.hereNow("my_channel", callback);
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
Callback callback = new Callback() {
    @Override
    public void connectCallback(String channel, Object message) {
       System.out.println("CONNECT on channel:" + channel
                + " : " + message.getClass() + " : "
                + message.toString());
    }

    @Override
    public void disconnectCallback(String channel, Object message) {
        System.out.println("DISCONNECT on channel:" + channel
                + " : " + message.getClass() + " : "
                + message.toString());
    }

    @Override
    public void reconnectCallback(String channel, Object message) {
        System.out.println("RECONNECT on channel:" + channel
                + " : " + message.getClass() + " : "
                + message.toString());
    }

    @Override
    public void successCallback(String channel, Object message) {
        System.out.println(channel + " : "
                + message.getClass() + " : " + message.toString());
    }

    @Override
    public void errorCallback(String channel, PubnubError error) {
        System.out.println("ERROR on channel " + channel
                + " : " + error.toString());
    }
};

try {
    pubnub.presence("my_channel", callback);
} catch (PubnubException e) {
    System.out.println(e.toString());
}
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
Callback callback = new Callback() {
  public void successCallback(String channel, Object response) {
    System.out.println(response.toString());
  }
  public void errorCallback(String channel, PubnubError error) {
	System.out.println(error.toString());
  }
};
pubnub.history("history_channel", 100, true, callback);
Stop subscribing (listening) to a channel.
pubnub.unsubscribe("my_channel");