I've trying all day to figure out a way to remove a Twilio CallbackListener from a Twilio channel when requesting unconsumed message count to avoid leaking memory when my activity is destroyed. Calling channel.removeAllListeners seems to only remove ChannelListener from the channel. I've posted some code below of what I'm attempting to do and here is a link to the Twilio chat docs. Any ideas?
CallbackListener<Long> callbackListener = new CallbackListener<Long>() {
@Override
public void onSuccess(Long unconsumedCount) {
}
@Override
public void onError(ErrorInfo errorInfo) {
super.onError(errorInfo);
}
};
channel.getUnconsumedMessagesCount(callbackListener);
Twilio developer evangelist here.
channel.getUnconsumedMessagesCount
only sets up the callbackListener
to be called once when the result is returned. It doesn't permanently set it up as a listener, so you are not leaking memory, and if you need to get the unconsumed message count again you need to call it again. Note that the value is cached for 5 seconds, so there is no need to call it any more often than that.
Let me know if that helps at all.