The Zendesk SDK for Android provides an event listener ZendeskEventListener where you can listen for any ZendeskEvent emitted from the SDK.

Available Events

ZendeskEvent is a sealed class with a subclass for each event that can currently be emitted. It has the following events:

UnreadMessageCountChanged

The number of unread messages has changed.

NameTypeMandatoryComment
idStringYesThe id of the event.
timestampStringYesThe Unix timestamp of the event.
dataConversationUnreadCountChangeYesShows the change in total unread messages and the change in unread messages for a conversation.
currentUnreadCountIntYesThe current number of unread messages. This is deprecated, use data instead.

AuthenticationFailed

Invoked when an authentication error has occurred on any of the API calls.

NameTypeMandatoryComment
errorErrorYesDetails about the error that occurred.

FieldValidationFailed

Invoked when validation checks fails for conversation fields.

NameTypeMandatoryComment
errorsListYesDetails about errors that occurred.

ConnectionStatusChanged

Invoked when The SDK [ConnectionStatus] has changed due to an action or another event.

NameTypeMandatoryComment
connectionStatusConnectionStatusYesA representation of the current connection status of the SDK.

SendMessageFailed

Invoked when a message fails to be sent.

NameTypeMandatoryComment
causeThrowableYesDetails about the error that occurred.

ConversationAdded

Invoked when a conversation has been added.

NameTypeMandatoryComment
conversationIdStringYesThe id of the conversation that was added.

ConversationStarted

Invoked when a user starts a conversation on this device. The user can initiate it by clicking the new conversation button, receiving and clicking on a proactive message, or creating a default conversation.

NameTypeMandatoryComment
idStringYesThe id of the event.
conversationIdStringYesThe id of the conversation that was started.
timestampLongYesThe Unix timestamp of the event.

ConversationOpened

Invoked when the user opens a conversation.

NameTypeMandatoryComment
idStringYesThe id of the event.
conversationIdStringYesThe id of the conversation that was opened.
timestampLongYesThe Unix timestamp of the event.

MessagesShown

Invoked whenever the messages shown to the user are updated.

NameTypeMandatoryComment
idStringYesThe id of the event.
conversationIdStringYesThe id of the conversation that the messages belong to.
timestampLongYesThe Unix timestamp of the event.
messagesListYesThe list of messages that are displayed.

EventListener

The EventListener can be created and attached to the current Zendesk instance using zendesk.addEventListener. To avoid missing events, make sure the Zendesk instance is fully initialized and valid before adding the listener.

A reliable approach is to call zendesk.addEventListener within the successCallback of the Zendesk.initialize method. This ensures the listener is added to the most current SDK instance.

You can find an example of this implementation within our demo apps:

Kotlin

Here is a code sample showing how the event listener can be used, added and removed:

// To create and use the event listener:val zendeskEventListener: ZendeskEventListener = ZendeskEventListener {    zendeskEvent -> when (zendeskEvent) {        is ZendeskEvent.UnreadMessageCountChanged ->{            // Your custom action...        }        is ZendeskEvent.AuthenticationFailed -> {            // Your custom action...        }        is ZendeskEvent.FieldValidationFailed -> {            // Your custom action...        }        else -> {           // Default branch for forward compatibility with Zendesk SDK and its `ZendeskEvent` expansion        }    }}
// To add the event listener to your Zendesk instance:// (safe for concurrent use)zendesk.addEventListener(zendeskEventListener)
// To remove the event listener from your Zendesk instance:// (safe for concurrent use)zendesk.removeEventListener(zendeskEventListener)

Java

Below is a code sample showing how the event listener can be used, added and removed.

This includes the ZendeskEventListenerAdapter, an implementation of the ZendeskEventListener designed to provide better support for Java:

// To create and use the event listener:ZendeskEventListener zendeskEventListener =    new ZendeskEventListener() {        @Override        public void onEvent(@NonNull ZendeskEvent zendeskEvent) {            if (zendeskEvent instanceof UnreadMessageCountChanged) {                // Your custom action...            } else if (zendeskEvent instanceof AuthenticationFailed){                // Your custom action...            } else if {zendeskEvent instanceof FieldValidationFailed} {                // Your custom action...            }        }    };
// Or using the ZendeskEventListenerAdapter:ZendeskEventListenerAdapter listenerAdapter = new ZendeskEventListenerAdapter() {    @Override    public void onUnreadMessageCountChanged(@NonNull UnreadMessageCountChanged event) {        super.onUnreadMessageCountChanged(event);        // Your custom action    }};
// To add the event listener to your Zendesk instance:zendesk.addEventListener(zendeskEventListener);
// To remove the event listener from your Zendesk instance:zendesk.removeEventListener(zendeskEventListener);

EventFlow

Below is a code sample which shows the usage of the zendesk.eventFlow API:

lifecycleScope.launch {    zendesk.eventFlow.collect { zendeskEvent ->        when (zendeskEvent) {            is ZendeskEvent.UnreadMessageCountChanged -> // Your custom action            else -> Unit        }    }}