Skip to content

Instantly share code, notes, and snippets.

@oisin
Last active December 26, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisin/7103220 to your computer and use it in GitHub Desktop.
Save oisin/7103220 to your computer and use it in GitHub Desktop.
// Android apps include Converser initialization and subscription
// as part of an activity. In the code snippet below, it shows useful
// places to put the code so that it is initialised and subscribes at
// the right time.
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// When this activity is created, initialize Converser with the correct key and endpoint
// this doesn't actually connect to the server, it just sets it up ready to go for the
// subscription later on when the activity starts.
//
ConverserEngine.init( this , getString(R.string.converser_key), getString(R.string.converser_endpoint));
}
@Override
protected void onStart() {
// Kick off the Converser session here with a subscribe to the service.
// do this here rather than in the create, so that your subscription state
// is checked when the activity restarts.
if (engine == null) {
engine = new ConverserEngine(getApplicationContext());
}
// Here's an example of how to use the subscribe call with error and
// success callbacks. Once the subscription has succeeded, you are ready
// to check the inbox and see if there's anything to grab.
// If there's a fail, you should retry. Note that it's no harm to do
// multiple subscribes in one session!
//
if (!engine.isSubscribed()) {
converserEngine.subscribe(getApplicationContext(), identity, null,
new ConverserEngine.Callback<Void>() {
@Override
public void onError(String error) {
Log.d("ConverserDemo", "An error occured");
}
@Override
public void onSuccess(Void nothing) {
Log.d("ConverserDemo", "Subscribe Succeeded!");
}
}
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment