How to Retrieve Undelivered Messages Saved in the Database for a Blocked User in SendBird?

[Question]
I am working with the SendBird Android SDK, and my use case involves sending messages in a group channel. If the recipient has blocked me, the messages are not delivered to the channel, which aligns with the documentation:

“Even though messages sent from the blocked user aren’t delivered to the channel, they are saved in the database and are only displayed in the blocked user’s channel view. This means that the blocked user isn’t aware of their blocked status”

My question is: since these messages are saved in the database, how can I retrieve them when fetching all the messages for the group channel?

I am using below code to retrieve messages in a channel, but these undelivered messages are not being retrieved with this

messageCollection?.initialize(
                        MessageCollectionInitPolicy.CACHE_AND_REPLACE_BY_API,
                        object : MessageCollectionInitHandler {
                            private var shouldSendErrorOnApiFail = true
                            override fun onApiResult(apiResultList: List<BaseMessage>?, e: SendbirdException?) {
                                if (e != null) {
                                
                                } 
                                val messagesList = mutableListOf<ChatBaseMessage>()
                                apiResultList?.map { baseMessage ->
                                    val chatMessage: ChatBaseMessage? =
                                        chatMapper.mapToChatMessage(baseMessage)
                                    chatMessage?.let { messagesList.add(it) }
                                }
                            }

                            override fun onCacheResult(cachedList: List<BaseMessage>?, e: SendbirdException?) {
                            }
                        }
                    )

To retrieve messages sent by a blocked user, you need to use the MessageListParams to include messages from blocked users. By default, these messages are not included. Adjust your code to include all messages, regardless of block status:

val params = MessageListParams().apply {
    includeMetaArray = true
    includeReactions = true
    reverse = false
    nextResultSize = 100
    isInclusive = true
    // This is the key part; ensure it's set to true
    includeMessagesFromBlockedUsers = true
}

messageCollection?.initialize(
    MessageCollectionInitPolicy.CACHE_AND_REPLACE_BY_API,
    object : MessageCollectionInitHandler {
        override fun onApiResult(apiResultList: List<BaseMessage>?, e: SendbirdException?) {
            if (e != null) {
                // Handle error
                return
            } 
            val messagesList = mutableListOf<ChatBaseMessage>()
            apiResultList?.map { baseMessage ->
                val chatMessage: ChatBaseMessage? = chatMapper.mapToChatMessage(baseMessage)
                chatMessage?.let { messagesList.add(it) }
            }
        }

        override fun onCacheResult(cachedList: List<BaseMessage>?, e: SendbirdException?) {
            // Handle cached result if needed
        }
    }
)

Ensure you set includeMessagesFromBlockedUsers to true when using MessageListParams. This will allow you to fetch all messages, including those sent by blocked users.

Hi @jonathan58, thanks for your response, but these attributes are not available:

val params = MessageListParams().apply {
            includeMetaArray = true
            includeReactions = true
            reverse = false
            nextResultSize = 100
            isInclusive = true
            // This is the key part; ensure it's set to true
            includeMessagesFromBlockedUsers = true
        }

from above code base, includeMetaArray, includeReactions, isInclusive, includeMessagesFromBlockedUsers attributes are not available

If the attributes you’re trying to use in MessageListParams are not available, it might be due to the version of the library you’re using or they might not be supported in your current setup. Here’s a revised approach that you can try if those attributes are missing:

val params = MessageListParams().apply {
    // Check the documentation or API for available attributes
    reverse = false
    nextResultSize = 100
    
    // If includeMessagesFromBlockedUsers is not available, you may need to handle this differently,
    // possibly by adjusting your API settings or checking for updates in the SDK.
}

// Initialize the message collection as before
messageCollection?.initialize(
    MessageCollectionInitPolicy.CACHE_AND_REPLACE_BY_API,
    object : MessageCollectionInitHandler {
        override fun onApiResult(apiResultList: List<BaseMessage>?, e: SendbirdException?) {
            if (e != null) {
                // Handle error
                return
            }
            val messagesList = mutableListOf<ChatBaseMessage>()
            apiResultList?.map { baseMessage ->
                val chatMessage: ChatBaseMessage? = chatMapper.mapToChatMessage(baseMessage)
                chatMessage?.let { messagesList.add(it) }
            }
        }

        override fun onCacheResult(cachedList: List<BaseMessage>?, e: SendbirdException?) {
            // Handle cached result if needed
        }
    }
)

Suggestions:

Ensure you are using the latest version of the SDK and consult its documentation. Sometimes, attributes might be added or removed in newer versions.