Advanced integration

When a user taps a URL, phone number, or email address sent to a conversation, the SDK handles the event automatically by launching the default app capable of completing the action:

  • the default browser for a URL
  • the default phone app for a phone number
  • the default email app for an email address

Note: Only URLs such as https://www.zendesk.com/ are made clickable in the UI. Other URIs such as zendesk://myapp will not be clickable.

You can customize the behavior and change what happens when the user clicks a URL by setting a MessagingDelegate. The shouldHandleUrl function will be called any time the user clicks a URL.

To prevent the SDK from opening the default browser, you should return false when this happens. In that case, you must handle the completion of the action yourself. The function receives two parameters:

  • url: the URL that was clicked
  • urlSource: an enumeration that describes where in the UI the url was clicked, such as a text message, a carousel item, and so on.

The following snippets show how you can set a MessagingDelegate in Kotlin and Java:

Kotlin

Messaging.setDelegate(object : MessagingDelegate() {    override fun shouldHandleUrl(url: String, urlSource: UrlSource): Boolean {        // Your custom action...
        // Return false to prevent the SDK from handling the URL automatically        // Return true to allow the SDK to handle the URL automatically, even        // if you have done something custom        return false    }})

Java

Messaging.setDelegate(new MessagingDelegate() {    @Override    public boolean shouldHandleUrl(@NotNull String url, @NotNull UrlSource urlSource) {        // Your custom action...
        // Return false to prevent the SDK from handling the URL automatically        // Return true to allow the SDK to handle the URL automatically, even        // if you have done something custom        return false;    }});

Events

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.

Name Type Comment
currentUnreadCount Int The current number of unread messages.

EventListener

Kotlin

Here is a code sample showing how the 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...        }        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 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...            }        }
    };    // 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        }    }}