Getting the Inbox with the Android SDK

After authenticating, the SDK will fetch all the recent conversations associated with the user and persist them in our local datastore. Assuming that the SDK has been properly initialized and "synced", this is the way you load your roster.

  1. Call getInboxEntries method of the RosterManager to return the list of all the available conversations.

    ...
    // After your SDK has been sync'ed you can call...
    TT.getInstance().getRosterManager().getInboxEntries("organizationId", new GenericRosterListener() {
    @Override
    public void onResults(List<RosterEntry> list) {
    // Populate your roster...
    // Will register for events in the next step...
    }
    });
  2. In order to keep your roster list up to date after loading the items, you need to subscribe to some roster related events that the SDK will fire when actions like "Roster Added", "Roster Removed", "Roster Updated", etc take place, in addition to the example above notice the last line in the "onResults" method below.

    ...
    // Declare roster related events required...
    private static final String[] rosterListeners = new String[]{
    TTEvent.ROSTER_ENTRY_ADDED,
    TTEvent.ROSTER_ENTRY_UPDATED,
    TTEvent.ROSTER_ENTRY_REMOVED
    };
    ...
    // After your SDK has been sync'ed you can call...
    TT.getInstance().getRosterManager().getInboxEntries("organizationId", new GenericRosterListener() {
    @Override
    public void onResults(List<RosterEntry> list) {
    // Populate your roster...
    // Subscribe to roster related events...
    TT.getInstance().getTTPubSub().addListeners(RosterActivity.this, rosterListeners);
    }
    });
  3. Now that we have our roster loaded and we are ready to start listening for roster related events, you are all set for a completely functional roster screen, below is the example of how your RosterActivity might look like.

    public class RosterActivity extends AppCompatActivity implements TTPubSub.Listener {
    //Declare the roster events required...
    private static final String[] rosterListeners = new String[]{
    TTEvent.ROSTER_ENTRY_ADDED,
    TTEvent.ROSTER_ENTRY_UPDATED,
    TTEvent.ROSTER_ENTRY_REMOVED
    };
    ...
    private void loadRosterAndRegisterForEvents() {
    TT.getInstance().getRosterManager().getInboxEntries("organizationId", new GenericRosterListener() {
    @Override
    public void onResults(List<RosterEntry> list) {
    //Populate recycler view roster elements...
    //Start listening for roster related events
    TTPubSub.getInstance().addListeners(RosterActivity.this, rosterListeners);
    }
    });
    }
    //This method will be called asynchronously every time the SDK fires an event related to the actions you registered on your pubsub...
    @Override
    public void onEventReceived(String event, Object data) {
    switch (event) {
    case TTEvent.ROSTER_ENTRY_ADDED: {
    final RosterEntry rosterEntry = (RosterEntry) data;
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    //Do something with the newly created roster entry (add it to the list?)
    }
    });
    }
    break;
    case TTEvent.ROSTER_ENTRY_REMOVED: {
    final RosterEntry rosterEntry = (RosterEntry) data;
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    //Do something with the removed roster entry (delete it from the list?)
    }
    });
    }
    break;
    ...//More events...
    }
    }
    //When your activity is destroyed don't forget to stop listening for events...
    @Override
    protected void onDestroy() {
    super.onDestroy();
    TT.getInstance().getTTPubSub().removeListeners(RosterActivity.this, rosterListeners);
    }
    }
  4. Loading the messages in a conversation is very similar to the example above, the only difference is that you would be using the ConversationManager to get the messages (paginated) and the events you will have to subscribe to will be message related events, like "New Message", "Message Recalled", "Message Failed", etc, The list of events fired by the SDK can be found in the TTEvent class.

    TT.getInstance().getConversationManager().getMessagesByPage(rosterEntry, pageSize, topMessage, new GenericActionListener<List<Message>, Throwable>() {
    @Override
    public void onResult(List<Message> messages) {
    // Update your Conversation view with these new messages
    messagesLiveData.postValue(messages);
    }
    @Override
    public void onFail(Throwable throwable) {
    Timber.e(throwable, "Failed getting messagesLiveData by page");
    }
    });