Here we list all API calls for the PubNub class in Java. This API is 100% compatible with all Java VMs including Android Phones, Google App Engine, Windows, Mac and Linux. This provides a complete Publish + Subscribe Push Notifications Framework in Java.
Download Java Push API on GitHubPubnub pubnub = new Pubnub( 'publish_key', 'subscribe_key' );
Get your API Keys. You only need the Publish and Subscribe Keys. Ignore the Secret Key.
// pubnub.publish( channel, message )
// Create Pubnub Object
Pubnub pubnub = new Pubnub( "demo", "demo" );
String channel = "java_test_channel";
// Create JSON Message
JSONObject message = new JSONObject();
try { message.put( "some_val", "Hello World!" ); }
catch (org.json.JSONException jsonError) {}
// Publish Message
JSONObject response = pubnub.publish( channel, message );
// Print Response from PubNub JSONP REST Service
System.out.println(response);
System.out.println(response.optString("status"));
System.out.println(response.optString("message"));
// pubnub.subscribe( channel, callback )
// Create Pubnub Object
Pubnub pubnub = new Pubnub( "demo", "demo" );
String channel = "java_test_channel";
// 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( channel, message_receiver );
// pubnub.history( channel, limit )
// Create Pubnub Object
Pubnub pubnub = new Pubnub( "demo", "demo" );
String channel = "java_test_channel";
int limit = 10;
// Publish Message
JSONArray response = pubnub.history( channel, limit );
// Print Response from PubNub JSONP REST Service
System.out.println(response);
System.out.println(response.optJSONObject(0).optString("some_val"));
That's it! Super simple right? Download Java Push API on GitHub