Skip to main content
This page contains Android SDK code examples for User Experience extensions. For feature documentation, setup instructions, and extension settings, see User Experience Extensions.

How to Use Extensions with SDK

1

Enable in Dashboard

Login to CometChat Dashboard, select your app, then go to Chat & Messaging → Features and enable the extension.
2

Configure settings (if required)

Some extensions require API keys or additional configuration. Open the extension settings in the Dashboard.
3

Implement SDK methods

Use the code examples below to call extension APIs and extract extension data from messages.
4

Build your UI

Create UI components to display extension data (e.g., link previews, pinned messages, saved messages).

Bitly

Shorten long URLs in messages using Bitly.

Shorten URL

val url = "/v1/shorten"
val body = JSONObject()
body.put("text", "Your message with URL https://yourdomain.com/very/very/long/url")

CometChat.callExtension("url-shortener-bitly", "POST", url, body,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(jsonObject: JSONObject) {
            // minifiedText from the extension
        }
        override fun onError(e: CometChatException) {
            // Some error occurred
        }
    }
)

Extract link preview metadata from messages.
val metadata = message.metadata
if (metadata != null) {
    val injectedObject = metadata.getJSONObject("@injected")
    if (injectedObject != null && injectedObject.has("extensions")) {
        val extensionsObject = injectedObject.getJSONObject("extensions")
        if (extensionsObject != null && extensionsObject.has("link-preview")) {
            val linkObject = extensionsObject.getJSONObject("link-preview")
            val linkArray = linkObject.getJSONArray("links")
            val linkPreviewObject = linkArray.getJSONObject(0)
            if (linkPreviewObject.has("description")) {
                val description = linkPreviewObject.getString("description")
            }
            // ... other fields
        }
    }
}

Message Shortcuts

Send predefined messages using shortcuts (e.g., !hb expands to “Happy birthday!”).

Fetch All Shortcuts

CometChat.callExtension("message-shortcuts", "GET", "/v1/fetch", null,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(responseObject: JSONObject) {
            // Shortcuts received here.
        }
        override fun onError(e: CometChatException) {
            // Some error occurred.
        }
    }
)

Modify Shortcuts

val body = JSONObject()
val shortcuts = JSONObject()
shortcuts.put("!hbd", "Happy birthday! Have fun!")
body.put("shortcuts", shortcuts)

CometChat.callExtension("message-shortcuts", "POST", "/v1/update", body,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(responseObject: JSONObject) {
            // Shortcuts updated successfully
        }
        override fun onError(e: CometChatException) {
            // Some error occurred
        }
    }
)

Pin Message

Pin important messages in conversations.

Pin a Message

val body = JSONObject()
body.put("msgId", 280)

CometChat.callExtension("pin-message", "POST", "/v1/pin", body,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(responseObject: JSONObject) {
            // { success: true }
        }
        override fun onError(e: CometChatException) {
            // Error occurred
        }
    }
)

Unpin a Message

val body = JSONObject()
body.put("msgId", 111)
body.put("receiverType", "group")
body.put("receiver", "cometchat-guid-1")

CometChat.callExtension("pin-message", "DELETE", "/v1/unpin", body,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(responseObject: JSONObject) {
            // { success: true }
        }
        override fun onError(e: CometChatException) {
            // Error occurred
        }
    }
)

Fetch Pinned Messages

val receiverType = "group"
val receiver = "cometchat-guid-1"
val url = "/v1/fetch?receiverType=$receiverType&receiver=$receiver"

CometChat.callExtension("pin-message", "GET", url, null,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(responseObject: JSONObject) {
            // { pinnedMessages: [] }
        }
        override fun onError(e: CometChatException) {
            // Error occurred
        }
    }
)

Rich Media Preview

Extract rich media preview metadata from messages using Iframely.

Extract Rich Media Data

val metadata = message.metadata
if (metadata != null) {
    val injectedObject = metadata.getJSONObject("@injected")
    if (injectedObject != null && injectedObject.has("extensions")) {
        val extensionsObject = injectedObject.getJSONObject("extensions")
        if (extensionsObject != null && extensionsObject.has("rich-media")) {
            val richMediaObject = extensionsObject.getJSONObject("rich-media")
            // Use richMediaObject data
        }
    }
}

Save Message

Save important messages for later access.

Save a Message

val body = JSONObject()
body.put("msgId", 111)

CometChat.callExtension("save-message", "POST", "/v1/save", body,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(responseObject: JSONObject) {
            // { success: true }
        }
        override fun onError(e: CometChatException) {
            // Error occurred
        }
    }
)

Unsave a Message

val body = JSONObject()
body.put("msgId", 111)

CometChat.callExtension("save-message", "DELETE", "/v1/unsave", body,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(responseObject: JSONObject) {
            // { success: true }
        }
        override fun onError(e: CometChatException) {
            // Error occurred
        }
    }
)

Fetch Saved Messages

CometChat.callExtension("save-message", "GET", "/v1/fetch", null,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(responseObject: JSONObject) {
            // { savedMessages: [] }
        }
        override fun onError(e: CometChatException) {
            // Error occurred
        }
    }
)

Thumbnail Generation

Extract thumbnail URLs from image and video messages.

Extract Thumbnail Data

val metadata = message.metadata
if (metadata != null) {
    val injectedObject = metadata.getJSONObject("@injected")
    if (injectedObject != null && injectedObject.has("extensions")) {
        val extensionsObject = injectedObject.getJSONObject("extensions")
        if (extensionsObject != null && extensionsObject.has("thumbnail-generation")) {
            val thumbnailObject = extensionsObject.getJSONObject("thumbnail-generation")
            val attachments = thumbnailObject.getJSONArray("attachments")
            for (i in 0 until attachments.length()) {
                val attachment = attachments.getJSONObject(i)
                if (!attachment.has("error")) {
                    val data = attachment.getJSONObject("data")
                    val thumbnails = data.getJSONObject("thumbnails")
                    val urlSmall = thumbnails.getString("url_small")
                    val urlMedium = thumbnails.getString("url_medium")
                    val urlLarge = thumbnails.getString("url_large")
                    // Use the URLs accordingly
                }
            }
        }
    }
}

TinyURL

Shorten long URLs in messages using TinyURL.

Shorten URL

val body = JSONObject()
body.put("text", "Your message with URL https://yourdomain.com/very/very/long/url")

CometChat.callExtension("url-shortener-tinyurl", "POST", "/v1/shorten", body,
    object : CometChat.CallbackListener<JSONObject>() {
        override fun onSuccess(jsonObject: JSONObject) {
            // minifiedText from the extension
        }
        override fun onError(e: CometChatException) {
            // Some error occurred
        }
    }
)

Voice Transcription

Extract transcribed text from audio messages.

Extract Transcription Data

val metadata = message.metadata
if (metadata != null) {
    val injectedObject = metadata.getJSONObject("@injected")
    if (injectedObject != null && injectedObject.has("extensions")) {
        val extensionsObject = injectedObject.getJSONObject("extensions")
        if (extensionsObject != null && extensionsObject.has("voice-transcription")) {
            val voiceTranscriptionObject = extensionsObject.getJSONObject("voice-transcription")
            val transcribedMessage = voiceTranscriptionObject.getString("transcribed_message")
            // Use the transcribed message
        }
    }
}