Send Message in a Thread
As mentioned in the Send a Message section, you can send a message to a User or a Group by mentioning the receiver (uid/guid) andreceiverType
(user/group).
A message can be categorized as:
- Text Message
- Media Message
- Custom Message.
- Interactive Message
parentMessageId
must be set for the message. This will indicate that the message to be sent has to be a part of the thread of the message with the specified parentMessageId
.
This can be achieved using the parentMessageId
parameter provided by the object of the TextMessage
, MediaMessage
and CustomMessage
class. The id specified in the parentMessageId
parameter maps the message sent to the particular thread.
Example to Send a Text Message in a thread in a user conversation.
parentMessageId
103.
Similarly, using the parentMessageId
parameter, Media and Custom Messages can be sent in threads too.
Receiving Real-Time Messages
The procedure to receive real-time messages is exactly the same as mentioned in the Receive Messages. This can be achieved using theMessageListener
class provided by the SDK.
To add a MessageListener, you can use the addMessageListener()
method of the SDK.
The only thing that needs to be checked is if the received message belongs to the active thread. This can be done using parentMessageId
field of the message object.
Fetch all the messages for any particular thread.
You can fetch all the messages belonging to a particular thread by using theMessagesRequest
class.
The MessageRequestBuilder
builds the MessageRequest
object using the following functions:
- parentMessageId: Takes the parentId of the message as argument whose thread needs to be requested.
- build(): returns the MessageRequest object.
MessagesRequest
object, you can call the fetchPrevious()
method to get the latest messages in the thread. In one iteration, a maximum of 100 messages can be fetched. If you wish to fetch the next set of messages, you need to call the fetchPrevious() method again on the same object.
Avoid Threaded Messages in User/Group Conversations
While fetching messages for normal user/group conversations using theMessagesRequest
, the threaded messages by default will be a part of the list of messages received. In order to exclude the threaded messages from the list of user/group messages, you need to use the hideReplies
parameter of the MessagesRequestBuilder
class. This method takes a boolean argument which when set to true excludes the messages belonging to threads from the list of messages.
cometchat-uid-1
excluding all the threaded messages belonging to the same conversation.