Dinesh Chitta Dinesh Chitta - 14 days ago 5
Android Question

getting null while calling for messages on current channel in Twilio chat android

Recently I updated my app to support recent version twilio chat 0.11.1.After that when I am calling currentChannel.getMessages() its returning null.
I am doing like

Channels channelsObject = basicClient.getIpMessagingClient
channelsObject.getChannel(channelId, new CallbackListener<Channel>() {
@Override
public void onSuccess(final Channel channel) {
currentChannel = channel;
setupRecyclerView();
}
});
private void setupRecyclerView() {
currentChannel.addListener(ChatFragment.this);
currentChannel.synchronize(new com.twilio.chat.CallbackListener<Channel>() {
@Override
public void onError(ErrorInfo errorInfo) {
Application.get().logErrorInfo("Channel sync failed", errorInfo);
}

@Override
public void onSuccess(Channel result) {
logger.d("Channel sync success for " + result.getFriendlyName());
}
});
Messages messagesObject = currentChannel.getMessages();


I got warnings like

| WARNING | ChatUtils(native) | ListenerWrapper default onSuccess() not found
| WARNING | Channel(native) | No messages available, maybe you forgot to synchronize the channel?

Answer

No messages available, maybe you forgot to synchronize the channel?

This is exactly what is happening. Channel will be synchronized only when you receive CallbackListener.onSuccess() callback. It is executed asynchronously, and your getMessages() call is executed before that callback fires.

      currentChannel.synchronize(new com.twilio.chat.CallbackListener<Channel>() {
          @Override
          public void onError(ErrorInfo errorInfo) {
            Application.get().logErrorInfo("Channel sync failed", errorInfo);
          }

          @Override
          public void onSuccess(Channel result) {
            logger.d("Channel sync success for " + result.getFriendlyName() + " now can get messages and members objects");
            Messages messagesObject = result.getMessages();
            // should be non-null now
          }
      });