---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# NowChatService class - Android

# NowChatService class - Android {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 3 minutes to read

The NowChatService class provides functions that enable you to launch the NowChat activity and set error configurations.

## NowChatService - launchIntent(context:Context, nowChatTheme:NowChatTheme):Intent {#ariaid-title2}

Launches the intent used to open the NowChat activity. Typically used for creating a
`[android.app.PendingIntent]`.
{#NChatServA-launchIntent_O_O__table_yv2_43l_nrb__entry__3}{#NChatServA-launchIntent_O_O__Chat-themeColors-ph}

| Name | Type | Description |
|-|-|-|
| context | Context | Context used to build the intent. |
| nowChatTheme | [NowChatTheme](https://servicenow-prod.fluidtopics.net/ijcWm3L259RTDrttEw0VFQ "The NowChatTheme interface defines default colors for the elements in the Live Agent and Virtual Agent chat UI.") | Optional. Theme object to use in the NowChat UI. Default: Default colors |
[Table 1. Parameters]

{#NChatServA-launchIntent_O_O__table_yv2_43l_nrb} {#NChatServA-launchIntent_O_O__table_zv2_43l_nrb__entry__2}

| Type | Description |
|-|-|
| Intent | Intent and theme colors used to launch the associated NowChat activity. |
[Table 2. Returns]

{#NChatServA-launchIntent_O_O__table_zv2_43l_nrb}  
This example shows how to call the launchIntent() method and process the
return activity.

    val intent = nowChatService.launchIntent(this) 
    val pendingIntent = PendingIntent.getActivity( 
        this, 
        REQUEST_CODE,
        intent,
        PendingIntent.FLAG_IMMUTABLE 
    ) 

    val notification = createNotification(title, message, pendingIntent)
    notificationManager.notify(push.notificationId, notification) 

## NowChatService - start(activity: Activity, themeColors: NowChatTheme = object: NowChatTheme{}, contextData: Map\<String, Any\> = mapOf(), chatConfiguration: NowChatConfiguration? = null) {#ariaid-title3}

Launches the specified NowChat activity.
{#NChatServA-start_O_O__table_lbg_cyg_tpb__entry__3}{#NChatServA-start_O_O__Chat-themeColors-ph}

| Name | Type | Description |
|-|-|-|
| activity | [Activity](https://developer.android.com/reference/kotlin/android/app/Activity.html) | Activity context to use to launch the NowChat activity and to receive the `NowChatExitCode` as a resultCode through the onActivityResult. |
| nowChatTheme | [NowChatTheme](https://servicenow-prod.fluidtopics.net/ijcWm3L259RTDrttEw0VFQ "The NowChatTheme interface defines default colors for the elements in the Live Agent and Virtual Agent chat UI.") | Optional. Theme object to use in the NowChat UI. Default: Default colors |
| contextData | Map\<String, Any\> | Optional. Additional chat context variables to pass into the chat session. For additional information on chat context variables, see [Live agent chat context variables](https://www.servicenow.com/docs/access?context=live-agent-chat-context-vars&version=xanadu&pubname=xanadu-conversational-interfaces&ft:locale=en-US). |
| chatConfiguration | [NowChatConfiguration](https://servicenow-prod.fluidtopics.net/M7o6dYMD~jFjeFbRCDfAyA#NowChatOptionsAndroid "The NowChatConfiguration class enables you to configure options on a chat session, such as showing a prompt before closing a chat window, disabling features while using chat, applying different conversation options when using chat, and configuring UI components in NowChat.") | Optional. ChatConfiguration to apply while using NowChat. |
[Table 3. Parameters]

{#NChatServA-start_O_O__table_lbg_cyg_tpb} {#NChatServA-start_O_O__table_mbg_cyg_tpb__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 4. Returns]

{#NChatServA-start_O_O__table_mbg_cyg_tpb}  
The following code example shows how to call this function.

    suspend fun launchChat() {
        val chatService = getNowChatService()

        val chatTheme = object : NowChatTheme {
            override val brand: Int
                get() = Color.BLUE

            override val textPrimary: Int
                get() = Color.BLACK

            // Override remaining theme colors

        }
        val contextData = mapOf("sys_id" to "123456789", "table" to "wm_task")

        //drawable to be used instead of the default NowChat back button.
        val myDrawable = ContextCompat.getDrawable(activity, R.drawable.my_drawable)

        val chatConfiguration = NowChatConfiguration(
            closePrompt = NowChatConfiguration.ClosePrompt(
                header = null,
                message = "Are you sure you want to leave?",
                acceptButtonTitle = "Yes",
                declineButtonTitle = "No"
            ),
            disabledFeatures = listOf(NowChatConfiguration.Feature.START_NEW_CONVERSATION),
            conversationOptions = listOf(NowChatConfiguration.ConversationOption.FORCE_NEW_CONVERSATION),
            uiConfiguration = NowChatConfiguration.UIConfiguration(
                closeButton = NowChatConfiguration.CloseButton(
                    icon = myDrawable
                ),
                attachmentUploadButton = NowChatConfiguration.AttachmentUploadButton(isVisible = false)
            )
        )

        chatService?.start(activity, chatTheme, contextData, chatConfiguration)
    }

## NowChatService - subscribeToUnreadMessageCount(pollingInterval: Long, listener:
NowChatUnreadMessagesCountListener) {#ariaid-title4}

Subscribes to the unread chat message count listener.
{#NChatServA-subToUnreadMsgCnt_I_O__table_zrg_k1m_cqb__entry__3}

| Name | Type | Description |
|-|-|-|
| pollingInterval | Long | Frequency at which to poll the web service for the unread chat message count. Unit: Milliseconds |
| listener | NowChatUnreadMessagesCountListener | Listener that you implement to obtain the number of unread chat messages. You must also unsubscribe to this listener when you no longer want to obtain the unread message count using the [NowChatService - unsubscribeFromUnreadMessageCount(listener: NowChatUnreadMessagesCountListener)](https://servicenow-prod.fluidtopics.net/Q3l5E6Rkr2057VkSlu6ktQ#NChatServA-unsubFromUnreadMsgCnt_O "Unsubscribes from receiving the unread message count.") method. |
[Table 5. Parameters]

{#NChatServA-subToUnreadMsgCnt_I_O__table_zrg_k1m_cqb} {#NChatServA-subToUnreadMsgCnt_I_O__table_asg_k1m_cqb__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 6. Returns]

{#NChatServA-subToUnreadMsgCnt_I_O__table_asg_k1m_cqb}  
The following example shows how to subscribe to and unsubscribe from a chat unread message
count listener.

    private val unreadMessageCountListener = object: NowChatUnreadMessagesCountListener {
      override fun unreadMessagesCountDidChange(unreadMessageCount: Int) {
      }
    }

    fun setup() {
      nowChatService.subscribeToUnreadMessageCount(pollingInterval:1000, unreadMessageCountListener)
    }

    fun teardown() {
      nowChatService.unsubscribeFromUnreadMessageCount(unreadMessageCountListener)
    }

## NowChatService - unsubscribeFromUnreadMessageCount(listener:
NowChatUnreadMessagesCountListener) {#ariaid-title5}

Unsubscribes from receiving the unread message count.
{#NChatServA-unsubFromUnreadMsgCnt_O__table_inp_hbm_cqb__entry__3}

| Name | Type | Description |
|-|-|-|
| listener | NowChatUnreadMessagesCountListener | Listener that you implement to unsubscribe from the chat unread message count listener. |
[Table 7. Parameters]

{#NChatServA-unsubFromUnreadMsgCnt_O__table_inp_hbm_cqb} {#NChatServA-unsubFromUnreadMsgCnt_O__table_jnp_hbm_cqb__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 8. Returns]

{#NChatServA-unsubFromUnreadMsgCnt_O__table_jnp_hbm_cqb}  
The following example shows how to subscribe to and unsubscribe from a chat unread message
count listener.

    private val unreadMessageCountListener = object: NowChatUnreadMessagesCountListener {
      override fun unreadMessagesCountDidChange(unreadMessageCount: Int) {
      }
    }

    fun setup() {
      nowChatService.subscribeToUnreadMessageCount(pollingInterval:1000, unreadMessageCountListener)
    }

    fun teardown() {
      nowChatService.unsubscribeFromUnreadMessageCount(unreadMessageCountListener)
    }

## NowChatService -- updateTheme(nowChatTheme: NowChatTheme) {#ariaid-title6}

Updates the NowChat UI theme with the specified UI theme. Use this function to update the chat UI theme after it has been initially set using the start() function, such as when changing the theme from light
to dark.
{#NCharServA-updateTheme_O__table_yzy_5jv_x2c__entry__3}

| Name | Type | Description |
|-|-|-|
| nowChatTheme | [NowChatTheme](https://servicenow-prod.fluidtopics.net/ijcWm3L259RTDrttEw0VFQ "The NowChatTheme interface defines default colors for the elements in the Live Agent and Virtual Agent chat UI.") | Theme object to use in the NowChat UI. |
[Table 9. Parameters]

{#NCharServA-updateTheme_O__table_yzy_5jv_x2c} {#NCharServA-updateTheme_O__table_zzy_5jv_x2c__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 10. Returns]

{#NCharServA-updateTheme_O__table_zzy_5jv_x2c}  
The following code example shows how to update a light UI theme implemented using the start() function to the dark UI theme using the updateTheme() function.

    val chatService = serviceManager.getNowChatService()

    val chatThemeLight = object : NowChatTheme {
        override val backgroundPrimary: Int
            get() = Color.WHITE

        override val textPrimary: Int
            get() = Color.BLACK

        // Override remaining theme colors
    }

    val chatThemeDark = object : NowChatTheme {
        override val backgroundPrimary: Int
            get() = Color.BLACK

        override val textPrimary: Int
            get() = Color.WHITE

        // Override remaining theme colors
    }

    //start NowChat with light theme
    chatService?.start(activity, chatThemeLight)


    //update NowChat theme to dark theme
    chatService?.updateTheme(chatThemeDark)


