twilio / twilio-chat-demo-android

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

How to load message by message id? #141

Closed emartynov closed 4 years ago

emartynov commented 4 years ago

Hi people.

I use Twilio Android SDK.

I'm getting push message with payload where I can get only message sid. I need message attributes that are not coming in the push (probably).

I use chatClient to load specific channel by sid. After I get messages object and it doesn't have method to load message by id. I can only load last x messages, or message with specific index.

What is your suggestion?

berkus commented 4 years ago

Use message index from the push payload to load message by index.

Fetching message by SID directly is in our plans, but requires finishing more prioritized work first.

emartynov commented 4 years ago

It looks like there is message index in payload, but it is not trivial to extract it.

Am I right that I have to use toString(), parse it as json and then I can obtain message index?

berkus commented 4 years ago

Clarification: current SDK does not have getMessageIndex() in the NotificationPayload implementation, but it is already added and will be available with the next released version.

For now it's quite easy to implement manually:

    public long getMessageIndex() {
        return payload.getLong("message_index", INVALID_MESSAGE_INDEX);
    }

edit: where payload is of Bundle type.

emartynov commented 4 years ago

How to get bundle? NotificationPayload doesn't have this api

berkus commented 4 years ago

You should be receiving a Bundle as remoteMessage from the push.

emartynov commented 4 years ago

Ah, I deal with api that already feed Bundle to NotificationPayload. So I have to go level deeper.

berkus commented 4 years ago

If you received it as a Map then do this:

JSONObject obj = new JSONObject(remoteMessage);
return obj.optLong("message_index", INVALID_MESSAGE_INDEX);
berkus commented 4 years ago

Again, this new NotificationPayload API is going to be released real soon now, so hopefully this change is temporary for you.

emartynov commented 4 years ago

Hopefully! Thank you! As for now I will go to this workarounds and implementation details :)

emartynov commented 4 years ago

Should I keep it open until you release new version or close it?

berkus commented 4 years ago

I think you can close it - it's already merged and will come out with the next release for sure.

emartynov commented 4 years ago

Closing!

berkus commented 4 years ago

Hi, we've released 6.0.0 with NotificationPayload.getMessageIndex()