NowChatService class - Android

  • Rversion finale: Australia
  • Mis à jour 12 mars 2026
  • 3 minutes de lecture
  • The NowChatService class provides functions that enable you to launch the NowChat activity and set error configurations.

    NowChatService - launchIntent(context:Context, nowChatTheme:NowChatTheme):Intent

    Launches the intent used to open the NowChat activity. Typically used for creating a [android.app.PendingIntent].

    Tableau 1. Parameters
    Name Type Description
    context Context Context used to build the intent.
    nowChatTheme NowChatTheme Optional. Theme object to use in the NowChat UI.

    Default: Default colors

    Tableau 2. Returns
    Type Description
    Intent Intent and theme colors used to launch the associated NowChat activity.

    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)

    Launches the specified NowChat activity.

    Tableau 3. Parameters
    Name Type Description
    activity Activity Activity context to use to launch the NowChat activity and to receive the NowChatExitCode as a resultCode through the onActivityResult.
    nowChatTheme NowChatTheme 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.

    chatConfiguration NowChatConfiguration Optional. ChatConfiguration to apply while using NowChat.
    Tableau 4. Returns
    Type Description
    None

    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)

    Subscribes to the unread chat message count listener.

    Tableau 5. Parameters
    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) method.
    Tableau 6. Returns
    Type Description
    None

    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)

    Unsubscribes from receiving the unread message count.

    Tableau 7. Parameters
    Name Type Description
    listener NowChatUnreadMessagesCountListener Listener that you implement to unsubscribe from the chat unread message count listener.
    Tableau 8. Returns
    Type Description
    None

    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)

    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.

    Tableau 9. Parameters
    Name Type Description
    nowChatTheme NowChatTheme Theme object to use in the NowChat UI.
    Tableau 10. Returns
    Type Description
    None

    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)