NowChatService クラス: Android

  • リリースバージョン: Zurich
  • 更新日 2025年07月31日
  • 所要時間:9分
  • NowChatService クラスは、NowChat アクティビティを起動してエラー構成を設定できる関数を提供します。

    NowChatService - launchIntent(context:Context, nowChatTheme:NowChatTheme):インテント

    NowChat アクティビティを開くために使用されるインテントを起動します。通常は [android.app.PendingIntent] の作成に使用されます。

    表 : 1. パラメーター
    名前 タイプ 説明
    コンテキスト コンテキスト インテントをビルドするために使用されるコンテキスト。
    Now チャットテーマ NowChatテーマ オプション。NowChat UI で使用するテーマオブジェクト。

    デフォルト:デフォルトの色

    表 : 2. 戻り値
    タイプ 説明
    インテント 関連する NowChat アクティビティを起動するために使用されるインテントとテーマの色。

    この例では、 launchIntent() メソッドを呼び出し、戻り値アクティビティを処理する方法を示します。

    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)

    指定された NowChat アクティビティを起動します。

    表 : 3. パラメーター
    名前 タイプ 説明
    アクティビティ Activity (アクティビティ) NowChat アクティビティを起動し、onActivityResult を介して NowChatExitCode を resultCode として受け取るために使用するアクティビティコンテキスト。
    Now チャットテーマ NowChatテーマ オプション。NowChat UI で使用するテーマオブジェクト。

    デフォルト:デフォルトの色

    コンテキストデータ map<string、任意> オプション。チャットセッションに渡す追加のチャットコンテキスト変数。

    チャットコンテキスト変数の詳細については、「 Live agent chat context variables」を参照してください。

    チャット構成 NowChatConfiguration オプション。 NowChat を使用する際に適用する ChatConfiguration。
    表 : 4. 戻り値
    タイプ 説明
    なし

    次のコード例は、この関数を呼び出す方法を示しています。

    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)

    未読チャットメッセージ数リスナーに登録します。

    表 : 5. パラメーター
    名前 タイプ 説明
    ポーリング間隔 未読チャットメッセージ数について Web サービスをポーリングする頻度。

    単位:ミリ秒

    リスナー NowChatUnreadMessagesCountListener 未読チャットメッセージの数を取得するために実装するリスナー。また、 NowChatService - unsubscribeFromUnreadMessageCount(リスナー: NowChatUnreadMessagesCountListener) メソッドを使用して未読メッセージ数を取得する必要がなくなった場合にも、このリスナーを登録解除する必要があります。
    表 : 6. 戻り値
    タイプ 説明
    なし

    次の例は、チャット未読メッセージカウントリスナーに登録および登録解除する方法を示しています。

    private val unreadMessageCountListener = object: NowChatUnreadMessagesCountListener {
      override fun unreadMessagesCountDidChange(unreadMessageCount: Int) {
      }
    }
    
    fun setup() {
      nowChatService.subscribeToUnreadMessageCount(pollingInterval:1000, unreadMessageCountListener)
    }
    
    fun teardown() {
      nowChatService.unsubscribeFromUnreadMessageCount(unreadMessageCountListener)
    }
    

    NowChatService - unsubscribeFromUnreadMessageCount(リスナー: NowChatUnreadMessagesCountListener)

    未読メッセージ数の受信を登録解除します。

    表 : 7. パラメーター
    名前 タイプ 説明
    リスナー NowChatUnreadMessagesCountListener チャットの未読メッセージ数リスナーを登録解除するために実装するリスナー。
    表 : 8. 戻り値
    タイプ 説明
    なし

    次の例は、チャット未読メッセージカウントリスナーに登録および登録解除する方法を示しています。

    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)

    NowChat UI テーマを指定された UI テーマで更新します。テーマを明から暗に変更する場合など、 start() 関数で最初に設定したチャット UI テーマを更新するには、この関数を使用します。

    表 : 9. パラメーター
    名前 タイプ 説明
    Now チャットテーマ NowChatテーマ NowChat UI で使用するテーマオブジェクト。
    表 : 10. 戻り値
    タイプ 説明
    なし

    次のコード例は、updateTheme() 関数を使用して、start() 関数を使用して実装された明るい UI テーマを暗い UI テーマに更新する方法を示しています。

    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)