twilio / twilio-chat-demo-android

Chat API Demo Application for Android
MIT License
62 stars 51 forks source link

Nested call back not getting result, want to get last message from each channel #159

Closed DasJhaman closed 3 years ago

DasJhaman commented 3 years ago

Description

Code (optional)

Using the following code for getting last message:

private fun getLastMessage(channel: Channels, dataList: List<Data>) {
    dataList.forEachIndexed { index, any ->
        channel.getChannel(
                any.remoteChannelId,
                ChatCallbackListener<Channel>() {
                    it!!.messages?.getLastMessages(1, ChatCallbackListener<List<Message>>() {
                        if (it.size > 0) {
                            if (it.get(0).hasMedia()) {
                                any.lastMessage = "attachment"
                            } else {
                                Log.e("last friend", it.get(0).messageBody)
                                any.lastMessage = it.get(0).messageBody
                            }
                        }
                    })
                })
    }
}

Expected Behavior

Get Last message of each channel

Actual Behavior

Not getting any of message loop getting execute

[What actually happens]

Reproduces how Often

[What percentage of the time does it reproduce?]

Logs

Please collect logs as described here. Full unedited logs help reproduce and fix issues faster.

// Log output when issue occurs

Or attach it as a file.

Logs

Yes, if you didn't collect the logs as described above, please collect the logs. Do not file the issue if you don't have enough information, this means having some logs, too.

Logs

Logs

Logs

Logs

Logs

Logs

Yes, seriously. Please send logs!

Versions

All relevant version information for issue, fill in below:

Chat Android SDK

Which SDK version did you use? [eg. 2.0.8]

Android API

On which Android API version or versions does it happen? [eg. 18]

Android Device

Vivo

Device or devices on which the problem happens [eg. Nexus 5]

berkus commented 3 years ago

I would suspect you have deliberately ignored ten lines asking to add logs?

berkus commented 3 years ago

But anyway, here's the problematic line:

ChatCallbackListener<Channel>() {
                    it!!.messages?.getLastMessages(1, ChatCallbackListener<List<Message>>() {

messages is null so it doesn't do anything. You should wait for complete channel synchronization first. See docs.

DasJhaman commented 3 years ago

How do I call channel synchronization, could you please share the documentation link.

berkus commented 3 years ago

Something like this and this.

DasJhaman commented 3 years ago
it.getUnconsumedMessagesCount(object : CallbackListener<Long>() {
                    override fun onSuccess(p0: Long?) {
                        if (p0 != null){
                            binding.tvUnReadMessagesCount.visibility = View.VISIBLE
                            binding.tvUnReadMessagesCount.text = ""+ p0

                        }
                    }

                })
DasJhaman commented 3 years ago

why unConsumed method not working as it giving me null.

berkus commented 3 years ago

@DasJhaman because you did not mark any messages as read, so by default they are all unread.

DasJhaman commented 3 years ago

I want to get the unread message count and want to display it on each chat room, which method is allowing so?

DasJhaman commented 3 years ago

@berkus by default they are all unread than it should return me the count, or any other method to get an unread count?

berkus commented 3 years ago

If it returns null you should query Channel.getMessagesCount().

Not an ideal usability but currently SDK is very explicit about the steps you need to do.

DasJhaman commented 3 years ago

as per document getMessageCount() return me the total message count in that chat thread, but I need only unread message count, how can I get this.

berkus commented 3 years ago

If all messages are unread, then you need the total message count - it equals unread message count (which equals ALL messages in the channel).

DasJhaman commented 3 years ago

If suppose i don't have all messages unread, only 2 are unread then?

berkus commented 3 years ago

Well, how many messages total do you have in the channel?

DasJhaman commented 3 years ago

More than 50

On Thu, Feb 11, 2021, 8:39 PM Berkus Decker notifications@github.com wrote:

Well, how many messages total do you have in the channel?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/twilio/twilio-chat-demo-android/issues/159#issuecomment-777585417, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6J5M3JFOMBIRR4JJIJQ73S6P233ANCNFSM4XNLZMYA .

berkus commented 3 years ago

But getMessagesCount() returns only 2??

DasJhaman commented 3 years ago

No it's returning me, total count.

berkus commented 3 years ago

So it's correct, you have total count unread messages, what do you complain about?

If you had only 2 unread messages this would mean you marked some messages as read, but you didn't, so they are still unread. All as designed.

Gray-Wind commented 3 years ago

You should mark at least one message as read with this method and then getUnconsumedMessagesCount will return actual unread message count.

berkus commented 3 years ago

By the way, its documented in detail in this method description, so make sure to read the documentation.

DasJhaman commented 3 years ago
`        private fun getLastMessageDate(chatThreadsitems: MutableList<Data>) {
    chatThreadsitems.forEachIndexed { index, any ->
        Log.e("text","looping $ "+index)
        hayiChatClient.chatClient!!.channels!!.getChannel(
            any.remoteChannelId,
            ChatCallbackListener<Channel>() {
                GlobalScope.launch {
                    it.waitForSynchronization()
                    any.channel = it
                    any.lastUpdatedAt = CommonMethods.getMileSeconds(it.lastMessageDate)
                }

            })
    }

    ProgressDialogBox.dismissDialog()
    sortAndSetItems(chatThreadsitems)
}`

@berkus @Gray-Wind some time my above method working fine and sometimes getting channel null, why this happening so, what is the best solution to deal with this. Thoug i am syncing the channel as well

berkus commented 3 years ago

It could be null because it's not found. Callback listener has onError() method - is it called?

DasJhaman commented 3 years ago

No, I am not using the onError method, I know it's null, why it's not found? Sometimes it's running perfectly some time making this issue? How can I execute this efficiency?

berkus commented 3 years ago

What do the logs say when it's null?

DasJhaman commented 3 years ago

E/text: looping $ 0 I/TWC: | Chat IPM | client [api] get channels D/TWC: | SDK EntityCache | Returning Java object from cache I/TWC: | Chat IPM | channels: [api] get channel 446_447_uniqueName D/TWC: | Chat IPM | [BFGDg] got channel from cache 446_447_uniqueName | Chat IPM | channels: got from cache 446_447_uniqueName E/text: looping $ 1 D/TWC: | SDK EntityCache | Returning Java object from cache I/TWC: | Chat IPM | client [api] get channels D/TWC: | SDK EntityCache | Returning Java object from cache I/TWC: | Chat IPM | channels: [api] get channel 437_447_uniqueName D/TWC: | Chat IPM | [1G2Ro] got channel from cache 437_447_uniqueName

DasJhaman commented 3 years ago

as per the loop, it's working perfectly but what is happening here my loop getting execute after that I am getting these logs. @berkus

berkus commented 3 years ago

Can I see the logs please?

DasJhaman commented 3 years ago
D/SurfaceExt: ENABLE_WHITE_LIST: false,isInWhiteList: false
D/Surface: lockCanvas
I/zygote64: Do partial code cache collection, code=1007KB, data=608KB
I/zygote64: After code cache collection, code=1007KB, data=608KB
    Increasing code cache capacity to 3MB
V/FA: Inactivity, disconnecting from the service
E/text: channel looping $ Fri Feb 19 16:52:34 GMT+05:00 2021
D/TWC:      | SDK Channel(native) | getSynchronizationStatus for channel sid|CHdea6bba7292e46e8be3350549f4c6159|
D/TWC:      | SDK Channel(native) | getSynchronizationStatus for channel sid|CHdea6bba7292e46e8be3350549f4c6159|
E/text: channel looping $ Thu Feb 18 23:07:08 GMT+05:00 2021
D/TWC:      | SDK Channel(native) | getSynchronizationStatus for channel sid|CHa953f9b77137464eadbfe1fd49c9915a|
         | SDK Channel(native) | getSynchronizationStatus for channel sid|CHa953f9b77137464eadbfe1fd49c9915a|
I/WebSocketWrapper: [1] onBinaryMessage: 96
I/WebSocketWrapper: [1] cancelTimer
    [1] startTimer: 43000
D/TWC:    3 | Twilsock Client | onMessageReceived, method = 2, id = TM8e64ee72138f4031a3f5d099a7f152a4
D/TWC:    3 | Twilsock Client | processPingMessage in state Connected , TM8e64ee72138f4031a3f5d099a7f152a4
       3 | Twilsock Client | sendReplyImpl in state Connected 
       3 | Twilsock Client | sendReply: 200, ok, TM8e64ee72138f4031a3f5d099a7f152a4, 
V/StudioTransport: Handling agent command 1200 for pid: 6453.
V/StudioTransport: Handling agent command 1200 for pid: 6453.
I/zygote64: Background concurrent copying GC freed 115352(5MB) AllocSpace objects, 0(0B) LOS objects, 36% free, 10MB/16MB, paused 281us total 160.787ms
V/StudioTransport: Handling agent command 1200 for pid: 6453.
V/StudioTransport: Handling agent command 1200 for pid: 6453.
V/StudioTransport: Handling agent command 1200 for pid: 6453.
V/StudioTransport: Handling agent command 1200 for pid: 6453.
V/FA: Recording user engagement, ms: 30712
V/FA: Connecting to remote service
E/: nFreeSize: 1394151424
V/FA: Activity paused, time: 231479982
V/StudioTransport: Handling agent command 1200 for pid: 6453.
V/FA: Connection attempt already in progress
D/FA: Connected to remote service
berkus commented 3 years ago

Proper trace level log should have at least 500 times more lines.

berkus commented 3 years ago

You can enable detailed logging and submit the resulting log file through help.twilio.com support (as it will contain some details of your account as well).

berkus commented 3 years ago

Here are the details on enabling the log level.

DasJhaman commented 3 years ago

@berkus link of submit log redirecting me to here https://www.twilio.com/

berkus commented 3 years ago

My bad, should be https://support.twilio.com/

DasJhaman commented 3 years ago

Is there any way to set twilio connection time out, if my wifi is off and i am going to connect twilio connection, webscoket is not calling onError method quickly, websocket holding the request more than 5 minutes. Is there any workaround? @berkus

Gray-Wind commented 3 years ago

Yes, there is a way. You need to use setCommandTimeout in options for a client creation.

DasJhaman commented 3 years ago

Thanks, @Gray-Wind, can you guide me why I am getting channel status UNKNOWN for the invited user in channel descriptor, and not getting channel if i use getChannel method from remoteChannelSid. Please guide me on how to get the invited status of the channel.

DasJhaman commented 3 years ago

Getting following error in log: D/TWC: | Http HttpResponse.ctor | payload '{"code":54007,"status":403,"message":"Access forbidden for identity"}' I/TWC: 172 | Sync TSCollectionOpenActivity | response status 403 body "{"code":54007,"status":403,"message":"Access forbidden for identity"}" (69 bytes) W/TWC: 171 | Sync List | request 37 error code 54007: Access forbidden for identity | Chat IPM | [yGAiy] messages: error 54007 Access forbidden for identity

berkus commented 3 years ago

No logs, closing.

If you still have this problem, please open a new ticket with proper full log.