How to get the last message of particular channel

Is there any way to get the latest message from particular channel

Hi @akshayg.stacksum ,

You can query messages (which will include latest messages) Getting and Viewing a Message - Amity Docs, and also get new messages in real time with our live objects https://docs.amity.co/core-concepts/live-objects-collections

If you can provide us with a more specific use case we may answer you better.

Thank you,
Amity Support

Is this a wrong way?

 channel.latestMessage().build().query().forEach {
               when (val data = it.getData()) {
                   is AmityMessage.Data.TEXT -> {
                       val text: String = data.getText()

                                   holder.tvChannelLastMessage.setText(text.split("\n").joinToString(" "))

                   }
                   is AmityMessage.Data.IMAGE -> {

                       holder.tvChannelLastMessage.setText("Sent an image")

                   }
                   is AmityMessage.Data.AUDIO -> {

                       holder.tvChannelLastMessage.setText("Sent an audio")

                   }
                   is AmityMessage.Data.FILE -> {

                       holder.tvChannelLastMessage.setText("Sent an file")

                   }
                   else -> {
                       Timber.d("lastMessageIS ")

                   }
               }


            }

Hi @akshayg.stacksum ,

May we please confirm your platform and version (that you want to get latest message on)?

Thank you,
Amity Support

platform is ANdroid and version is amity ui kit 6.3.0

@akshayg.stacksum Looking into this for you and will let you asap with any updates. Thank you

@akshayg.stacksum Please try this following code. To make this code work, you’ll have to enter “General” subchannel to fetch the messages first. (Or receiving a new message in General subchannel)

channel.latestMessage().build().query()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext {
               when (val data = it.getData()) {
                   is AmityMessage.Data.TEXT -> {
                       val text: String = data.getText()

                                   holder.tvChannelLastMessage.setText(text.split("\n").joinToString(" "))

                   }
                   is AmityMessage.Data.IMAGE -> {

                       holder.tvChannelLastMessage.setText("Sent an image")

                   }
                   is AmityMessage.Data.AUDIO -> {

                       holder.tvChannelLastMessage.setText("Sent an audio")

                   }
                   is AmityMessage.Data.FILE -> {

                       holder.tvChannelLastMessage.setText("Sent an file")

                   }
                   else -> {
                       Timber.d("lastMessageIS ")

                   }
               }
            }
            .subscribe()

Thank you,
Amity Support

Noting, solution provided is a workaround, the feature’s currently under development schedule by our team. We’ve taken note of your need for this function. Thank you

@amitysupport
thanks for provide the solution, uikit is now working, and displaying the joined channels.