Send and receive messages
After logging in to Chat, users can send the following types of messages to a peer user, a chat group, or a chat room:
- Text messages, including hyperlinks and emojis.
- Attachment messages, including image, voice, video, and file messages.
- Location messages.
- CMD messages.
- Custom messages.
The Chat message feature is language agnostic. End users can send messages in any language, as long as their devices support input in that language.
In addition to sending messages, users can also forward one or more messages. When forwarding multiple messages, users have the following options:
- Forward messages one-by-one
- Forward combined messages as message history
This page shows how to implement sending, receiving, forwarding multiple messages, and modifying sent messages using the Chat SDK.
Understand the tech
The Chat SDK uses the ChatManager
class to send, receive, and withdraw messages.
The process of sending and receiving a message is as follows:
- The message sender creates a text, file, or attachment message using the corresponding
create
method. - The message sender calls
sendMessage
to send the message. - The message recipient calls
addMessageListener
to listens for message events and receive the message in theOnMessageReceived
callback.
Prerequisites
Before proceeding, ensure that you meet the following requirements:
-
You have integrated the Chat SDK, initialized the SDK, and implemented the functionality of registering accounts and login. For details, see Chat SDK quickstart.
Use Chat SDK 1.2 or higher if you intend to enable users to forward multiple messages, or to modify sent messages.
-
You understand the API call frequency limits as described in Limitations.
Implementation
This section shows how to implement sending and receiving the various types of messages.
Send a text message
Use the ChatMessage
class to create a text message, and send the message.
Set message priority
In high-concurrency scenarios, you can set a certain message type or messages from a chat room member as high, normal, or low priority. In this case, low-priority messages are dropped first to reserve resources for the high-priority ones, for example, gifts and announcements, when the server is overloaded. This ensures that the high-priority messages can be dealt with first when loads of messages are being sent in high concurrency or high frequency. Note that this feature can increase the delivery reliability of high-priority messages, but cannot guarantee the deliveries. Even high-priorities messages can be dropped when the server load goes too high.
You can set the priority for all types of messages in the chat room.
Receive a message
You can use MessageListener
to listen for message events. You can add multiple MessageListener
s to listen for multiple events. When you no longer listen for an event, ensure that you remove the listener.
When a message arrives, the recipient receives an onMessagesReceived
callback. Each callback contains one or more messages. You can traverse the message list, and parse and render these messages in this callback.
Recall a message
After a message is sent, you can recall it. The recallMessage
method recalls a message that is saved both locally and on the server, whether it is a historical message, offline message or a roaming message on the server, or a message in the memory or local database of the message sender or recipient.
The default time limit for recalling a message is two minutes. You can extend this time frame to up to 7 days in Agora Console. To do so, select a project that enables Agora Chat, then click Configure > Features > Message recall.
You can also use onMessageRecalled
to listen for the message recall state:
Send and receive an attachment message
Voice, image, video, and file messages are essentially attachment messages. This section introduces how to send these types of messages.
Send and receive a voice message
Before sending a voice message, you should implement audio recording on the app level, which provides the URI and duration of the recorded audio file.
Refer to the following code example to create and send a voice message:
When the recipient receives the message, refer to the following code example to get the audio file:
Send and receive an image message
By default, the SDK compresses the image file before sending it. To send the original file, you can set original
as true
.
Refer to the following code example to create and send an image message:
When the recipient receives the message, refer to the following code example to get the thumbnail and attachment file of the image message:
ChatClient.getInstance().getOptions().getAutodownloadThumbnail()
is set as true
on the recipient's client, the SDK automatically downloads the thumbnail after receiving the message. If not, you need to call ChatClient.getInstance().chatManager().downloadThumbnail(message)
to download the thumbnail and get the path from the thumbnailLocalUri
member in messageBody
.
If the file does not exist in imgLocalUri
, you can manually call ChatClient.getInstance().chatManager().downloadAttachment(message)
to download the attachment, and call the message. SetMessageStatusCallback (the callback)
to see if the download succeeds.Send and receive a video message
Before sending a video message, you should implement video capturing on the app level, which provides the duration of the captured video file.
Refer to the following code example to create and send a video message:
By default, when the recipient receives the message, the SDK downloads the thumbnail of the video message.
If you do not want the SDK to automatically download the video thumbnail, set ChatClient.getInstance().getOptions().setAutodownloadThumbnail
as false
, and to download the thumbnail, you need to call ChatClient.getInstance().chatManager().downloadThumbnail(message)
, and get the path of the thumbnail from the thumbnailLocalUri
member in messageBody
.
To download the actual video file, call ChatClient.getInstance().chatManager().downloadAttachment(message)
, and get the path of the video file from the getLocalUri
member in messageBody
.
Send and receive a file message
Refer to the following code example to create, send, and receive a file message:
While sending a file message, refer to the following sample code to get the progress for uploading the attachment file:
When the recipient receives the message, refer to the following code example to get the attachment file:
ChatClient.getInstance().chatManager().downloadAttachment(message)
to download the attachment, and call the message. SetMessageStatusCallback (the callback)
to see if the download succeeds.Send a location message
To send and receive a location message, you need to integrate a third-party map service provider. When sending a location message, you get the longitude and latitude information of the location from the map service provider; when receiving a location message, you extract the received longitude and latitude information and displays the location on the third-party map.
Send and receive a CMD message
CMD messages are command messages that instruct a specified user to take a certain action. The recipient deals with the command messages themselves.
- CMD messages are not stored in the local database.
- Actions beginning with
em_
andeasemob::
are internal fields. Do not use them.
To notify the recipient that a CMD message is received, use a separate listener so that users can deal with the message differently.
Send a customized message
The following code example shows how to create and send a customized message:
Use message extensions
If the message types listed above do not meet your requirements, you can use message extensions to add attributes to the message. This can be applied in more complicated messaging scenarios.
Forward multiple messages
Supported types for forwarded messages include text, images, audio & video files, attachment, and custom messages. A user can create a combined message with a list of original messages and send it. When receiving a combined message, the recipient can select it and other messages to create a new layered combined message. A combined message can contain up to 10 layers of messages, with at most 300 messages at each layer.
To forward and receive combined messages, refer to the following code:
-
Create a combined message using multiple message IDs:
-
Download and parse combined messages:
For further details see Multiple messages forwarding limitations.
Modify sent messages
Every end user or chat group member may edit messages that they have sent. The client API below, when called, will allow the SDK to modify a message.
There is no time limit for modifying a message, that is, it can be modified as long as the message is still stored on the server. After the message is modified, the message life cycle, that is, its storage time on the server, is recalculated. For example, a message can be stored on the server for 180 days, and the user modifies it on the 30th day after the message was sent. Instead of remaining 150 days, the message can be now stored on the server for 180 days after successful modification.
In the modified message, the message ID remains unchanged. Only the message content is edited and the following items are added:
- The operator ID of the user performing the action.
- The operation time that indicates when the message was edited.
- The number of times a message is edited (up to 10 times).
For the edited message, except the message body, other information included in the message like the message sender, recipient, and message extension attributes remain unchanged.
To modify a sent message, refer to the following code:
-
Call
asyncModifyMessage
with the message ID and the new message body: -
Receive notification of messages modified by other users:
For further details see Sent message modification limitations.
Next steps
After implementing sending and receiving messages, you can refer to the following documents to add more messaging functionalities to your app: