Setting up delegates for links

Introduction

Click delegates are custom handlers that let you intercept and manage the SDK’s behavior when users tap clickable weblinks in a conversation. By default, the SDK automatically handles taps on weblinks by opening the default browser.

Use cases

Here are some common ways you can use click delegates to enhance the user experience in your app:

  • Deep linking: Intercept URLs to navigate users to specific screens.
  • Analytics: Log events when users click on any URL to track engagement.
  • Custom actions: Define custom behaviors, such as opening weblinks in a custom in-app browser.

Implementing click delegates

To customize how URL clicks are handled, you need to implement a MessagingDelegate and override the shouldHandleUrl function. This function is triggered every time a user clicks a URL in the conversation.

Within shouldHandleUrl, you decide whether the SDK should handle the URL or if your app will take over.

  • To let the SDK handle it: Return true. The SDK will handle the URL by launching the default browser. You can still perform custom actions before returning.
  • To handle it yourself: Perform your custom action, then return false. This prevents the SDK from handling the click and gives your app complete control over the action.

The shouldHandleUrl function provides the following parameters:

  • url: The URL string that was clicked.
  • urlSource: An enum that tells you where the URL was clicked in the UI, such as a text message or a carousel.

Here's how to implement the 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.        // Return true to let the SDK handle it.        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.        // Return true to let the SDK handle it.        return false;    }});

For a working example, see the Zendesk SDK Click Handler Demo App

FAQ

Q: What happens if no MessagingDelegate is implemented?
A: If you don't set a delegate, the SDK will handle all URL clicks automatically by opening them in the default browser.

Q: Is it possible to handle some URLs and let the SDK handle others?
A: Yes. Inside shouldHandleUrl, you can add conditional logic. For URLs you want to handle yourself, perform your action and return false. For all other URLs, return true to let the SDK handle them.

// Example: Handle only specific URLs from TEXT and IMAGE sourcesoverride fun shouldHandleUrl(url: String, urlSource: UrlSource): Boolean {    return when (urlSource) {        UrlSource.TEXT, UrlSource.IMAGE -> {            if (url.startsWith("https://www.zendesk.com/")) {                // Handle it yourself for TEXT and IMAGE sources only                false            } else {                // Let SDK handle other URLs from TEXT and IMAGE sources                true            }        }        // Let SDK handle all other URL sources by default        else -> true    }}