Events about new messages are not received in GroupChannel

Problem
Events about new messages are not received in GroupChannel


SDK Version: 4.1.3
Frequency: always
Current impact: I cannot get events about new messages

Reproduction Steps:
When I enter ChatScreen, I initiate sendbird in a coroutine via callbackFlow. After sendbird is initialized, I also add a channelHandler to another coroutine via callbackFlow, but when I send a message to the group channel via dashboard, I don’t get a new message event.

My current implementation:

SendbirdInitializer.kt

@Singleton
class SendbirdInitializer @Inject constructor(
  private val context: Context,
) {

  fun initIfNeeded(
    sendbirdAppId: String,
    sessionToken: String,
    userId: String,
  ): Flow<Unit> = callbackFlow {
    if (SendbirdChat.connectionState == ConnectionState.CLOSED) {
      val initResultHandler =
        object : InitResultHandler {
          override fun onInitSucceed() {
            SendbirdChat.connect(userId, sessionToken) { _, error ->
              if (error != null) {
                throw error
              } else {
                launch { send(Unit) }
              }
            }
          }

          override fun onMigrationStarted() {
            // TODO: handle migration started
          }

          override fun onInitFailed(e: SendbirdException) {
            // TODO: handle migration started
          }
        }

      SendbirdChat.init(
        initParams =
          InitParams(
            appId = sendbirdAppId,
            context = context,
            useCaching = false,
          ),
        handler = initResultHandler,
      )
      awaitClose { SendbirdChat.disconnect(handler = null) }
    }
  }
}

MessageApi.kt

internal class MessageApi @Inject constructor(
  private val channelProvider: SendbirdChannelProvider,
) {

  override fun observeNewMessages(): Flow<Message> = callbackFlow {
    val handler = object : GroupChannelHandler() {
      override fun onMessageReceived(channel: BaseChannel, message: BaseMessage) {
        Timber.tag("MessageApi").d("New message")
      }
    }
    SendbirdChat.addChannelHandler("HANDLER_ID", handler)
    awaitClose { SendbirdChat.removeChannelHandler("HANDLER_ID") }
  }
}

ChatActor.kt

internal class ChatActor @Inject constructor(
  private val messageApi: MessageApi,
  private val sendbirdInitializer: SendbirdInitializer,
) : Actor<Command, Internal> {

  override fun execute(command: Command): Flow<Internal> =
    when (command) {
      is Command.InitSendbird ->
        sendbirdInitializer
          .initIfNeeded(
            sendbirdAppId = command.sendbirdCredentials.sendbirdAppId,
            sessionToken = command.sendbirdCredentials.sessionToken,
            userId = SendbirdUserId,
          )
          .onEach {
            messageApi.observeNewMessages()
          }
}

I send a Command.InitSendbird to the Actor when I open the ChatScreen, and after sendbird successfully initializes, I add a GroupChannelHandler and my handler doesn’t receive new message events.

This only works when I wrote this code in onCreate method in MainActivity and don’t use coroutines. For example:

MainActivity.kt

class MainActivity : FragmentActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val sessionToken = "token"
    val sendbirdAppId = "sendbirdApp"
    val userId = "user_id"

    val groupChannelHandler = object : GroupChannelHandler() {
      override fun onMessageReceived(channel: BaseChannel, message: BaseMessage) {
        Timber.tag("MessageApi").d("I HAVE A MESSAGE!!!!")
      }
    }
    val initResultHandler =
      object : InitResultHandler {
        override fun onInitSucceed() {
          SendbirdChat.connect(userId, sessionToken) { _, error ->
            if (error != null) {
              throw error
            } else {
              SendbirdChat.addChannelHandler(
                "HANDLER_ID",
                groupChannelHandler,
              )
            }
          }
        }

        override fun onMigrationStarted() {}

        override fun onInitFailed(e: SendbirdException) {}
      }

    SendbirdChat.init(
      initParams =
      InitParams(
        appId = sendbirdAppId,
        context = this,
        useCaching = false,
      ),
      handler = initResultHandler,
    )
  }
}

In this implementation, the groupChannelHandler receives events about new messages, but I don’t understand why.
I decided that I could only add a groupChannelHandler to the main thread and used handler.post(), but that didn’t work for me, moreover, when I did it in MainActivity, my groupChannelHandler stopped receiving new message events.

MainActivity.kt

class MainActivity : FragmentActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val sessionToken = "token"
    val sendbirdAppId = "sendbirdApp"
    val userId = "user_id"
    val mainHandler = Handler(mainLooper)
    
    mainHandler.post {
      val groupChannelHandler = object : GroupChannelHandler() {
        override fun onMessageReceived(channel: BaseChannel, message: BaseMessage) {
          Timber.tag("MessageApi").d("I HAVE A MESSAGE!!!!")
        }
      }
      val initResultHandler =
        object : InitResultHandler {
          override fun onInitSucceed() {
            SendbirdChat.connect(userId, sessionToken) { _, error ->
              if (error != null) {
                throw error
              } else {
                SendbirdChat.addChannelHandler(
                  "HANDLER_ID",
                  groupChannelHandler,
                )
              }
            }
          }

          override fun onMigrationStarted() {}

          override fun onInitFailed(e: SendbirdException) {}
        }

      SendbirdChat.init(
        initParams =
        InitParams(
          appId = sendbirdAppId,
          context = this,
          useCaching = false,
        ),
        handler = initResultHandler,
      )
    }
  }
}

When I do it in the coroutine with Main dispatcher it not work too. I need to add groupChannelHandler from a coroutine.
Why my handler not get new events from a coroutine?