Testing out Pusher (pusher.com) message system / service by knocking up a simple Android client app using Android Studio 1.5.
Create a free account at https://pusher.com (link on top right) and make a note of the app_id and most importantly the key.
Pusher give a basic example of an Android client in the documentation : https://pusher.com/docs/android_quick_start , using the test account just created, a customized version (with your key in place) is available under your Pusher dashboard : https://dashboard.pusher.com/ , then click on the relevant App
A more in depth example from Pusher is in their blog : https://blog.pusher.com/build-an-android-chat-app-with-pusher/
In this post, I am going to knock up a basic client which will connect to the test channel and receive events. I used Android Studio (1.5).
Initial Project Setup
In build.gradle – project level, add the Mavern repository http://clojars.org/repo . Add this to buildscript repositories and allprojects repositories, like this (click image for readable screenshot):

When attempting to sync, if you have not added to allprojects, gradle will give the following error:
Error:Failed to resolve: org.java-websocket:java-websocket:1.3.1<a href=”openFile:/path/PusherTest/app/build.gradle”>Open File</a><br><a href=”open.dependency.in.project.structure”>Show in Project Structure dialog</a>
Solution is to add to allprojects.
You can add to allprojects through the Android Studio UI if preferred. In Android Studio, goto File -> Project Structure. In the resulting popup window goto ‘Project’, then add ‘http://clojars.org/repo’ after under ‘Default Library Repository’ after jcenter:

In the Module build.gradle, add pusher-java-client and android-async-http. The pusher-java-client info is given in Pusher’s android quick start. async-http is taken from the fuller example in Pusher’s blog.
If copying from Pusher’s blog (https://blog.pusher.com/build-an-android-chat-app-with-pusher/) you need to remove the extraneous commas from the gradle dependences section, so:
compile
'com.loopj.android:android-async-http:1.4.9'
,
// for our HTTP requests later
becomes
compile
'com.loopj.android:android-async-http:1.4.9'
// for our HTTP requests later
The module gradle should have the following added (first taken from pushers quick start, the second taken from pushers fuller example in the blog – chat app):

This gets a basic project to the stage where pusher API objects can be utilised.
Simple code to connect and display incoming messages
For this test I wanted a very simple client which connected to a test channel and simply display any incoming messages.
For debugging / logging I added implements of com.pusher.client.connection.ConnectionEventListener (very useful for seeing why the connect does not work!), and also com.pusher.client.channel.SubscriptionEventListener (to receive the events).
For simplicity and ease of reading, I implemented into the main activity, and created functions in the main activity class:
public class MainActivity extends AppCompatActivity implements SubscriptionEventListener, ConnectionEventListener{
Make sure ConnectionEventListener is the com.pusher type and not javax.sql.
The 3 methods related to the 2 interfaces are:
@Override
public void onEvent(String strChannel, String strEventName, String strData) {
// pusher Channel SubscriptionEventListener
showMessage("Pusher Event : " + strChannel + ":" + strEventName + ":" + strData);
}
@Override
public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {
// pusher Connection listener
showMessage("Connection State Change: "+connectionStateChange.getCurrentState().toString());
}
@Override
public void onError(String s, String s1, Exception e) {
// pusher Connection listener - on Error
showMessage("Connection Error: "+e.getMessage());
}
showMessage is a simple function which displays a Toast using runOnUiThread :
public void showMessage(final String strMessage) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast toast = Toast.makeText(getApplicationContext(), strMessage, Toast.LENGTH_LONG);
toast.show();
}
});
}
The imports for my main activity now have these additional imports:
import com.pusher.client.Pusher;
import com.pusher.client.PusherOptions;
import com.pusher.client.channel.Channel;
import com.pusher.client.channel.SubscriptionEventListener;
import com.pusher.client.connection.ConnectionEventListener;
import com.pusher.client.connection.ConnectionStateChange;
Add some private variables for object instances and also for the key, channel name etc (could have used string resource) :
private static String coPusherKey = "yourkeyvalue";
private static String coChannel = "my_channel";
private static String coEvent = "new_message";
private Pusher pushTest = null;
private Channel pushChannel = null;
private PusherOptions pushOptions = null;
Then in the onCreate for the activity, simply add the following code (the PusherOptions is not required – I added whilst testing)
pushOptions = new PusherOptions();
pushOptions.setEncrypted(true);
pushTest = new Pusher(coPusherKey, pushOptions);
pushTest.connect(this);
pushChannel = pushTest.subscribe(coChannel);
pushChannel.bind(coEvent,this);
Pusher.connect takes no parameters in the Pusher example – I added ‘this’ as implementing ‘ConnectionEventListener’.
The ConnectionEventListener is very useful as without this it is not apparent why the connection is not working (or even if it is working). Running through debugger pushTest.connection.state == ‘DISCONNECTED’.

Having the ConnectionEventListener enables visibility into the status changes and also through ‘onError’ to get information on the problem occurring when a connection fails. showMessage could/should write to log etc but visual testing, so using a Toast. Running gives the following:

This tells us that the Internet permission is needed for the Android application. Add the permission to the Manifest :
<uses-permission android:name="android.permission.INTERNET"/>

This is now a functioning Android application which connects to the channel and displays any events incoming.
To test open a browser and go to your Pusher account, then navigate to the debug console and enter a test message (I used the default). Finally click the ‘Send Event’ button. The message will appear in your running Android application.
The message then appears in the android app.
