hyphenate_SDK4.0 4.2.3-lite
hyphenate java IM SDK
Public Member Functions | Protected Member Functions | List of all members
com.hyphenate.chat.EMChatManager Class Reference

Public Member Functions

void sendMessage (final EMMessage msg)
 
void ackConversationRead (String conversationId) throws HyphenateException
 
void ackMessageRead (String to, String messageId) throws HyphenateException
 
void recallMessage (EMMessage message) throws HyphenateException
 
void asyncRecallMessage (final EMMessage message, final EMCallBack callback)
 
EMMessage getMessage (String messageId)
 
EMConversation getConversation (String id)
 
EMConversation getConversation (String id, EMConversationType type)
 
EMConversation getConversation (String username, EMConversationType type, boolean createIfNotExists)
 
EMConversation getConversation (String username, EMConversationType type, boolean createIfNotExists, boolean isChatThread)
 
int getUnreadMessageCount ()
 
void saveMessage (EMMessage message)
 
void downloadAttachment (final EMMessage msg)
 
void downloadThumbnail (final EMMessage msg)
 
List< EMConversationgetConversationsByType (EMConversationType type)
 
Map< String, EMConversationgetAllConversations ()
 
void asyncFetchPinnedConversationsFromServer (final int limit, final String cursor, final EMValueCallBack< EMCursorResult< EMConversation > > callback)
 
void asyncFetchConversationsFromServer (final int limit, final String cursor, final EMValueCallBack< EMCursorResult< EMConversation > > callback)
 
void asyncPinConversation (final String conversationId, boolean isPinned, final EMCallBack callback)
 
boolean deleteConversation (String username, boolean deleteMessages)
 
void deleteConversationFromServer (String username, EMConversationType type, boolean isDeleteServerMessages, EMCallBack deleteCallBack)
 
void addMessageListener (EMMessageListener listener)
 
void removeMessageListener (EMMessageListener listener)
 
void addConversationListener (EMConversationListener listener)
 
void removeConversationListener (EMConversationListener listener)
 
void setVoiceMessageListened (EMMessage message)
 
void asyncFetchHistoryMessages (String conversationId, EMConversationType type, int pageSize, String cursor, EMFetchMessageOption option, final EMValueCallBack< EMCursorResult< EMMessage > > callBack)
 

Protected Member Functions

void removeMessagesFromServer (String conversationId, EMConversationType type, long beforeTimeStamp, EMCallBack callBack)
 

Detailed Description

The chat manager class, responsible for sending and receiving messages, managing conversations (including loading and deleting conversations), and downloading attachments.

The sample code for sending a text message is as follows:

EMMessage message = EMMessage.createTxtSendMessage(content, toChatUsername);
EMClient.getInstance().chatManager().sendMessage(message);

Member Function Documentation

◆ ackConversationRead()

void com.hyphenate.chat.EMChatManager.ackConversationRead ( String  conversationId) throws HyphenateException

Sends the conversation read receipt to the server.

This method is only for one-to-one chat conversations.

This method will inform the sever to set the unread message count of the conversation to 0. The message sender will receive the EMConversationListener#onConversationRead(String, String) callback. The message recipient that has logged in to multiple devices, will also receive the callback.

Parameters
conversationIdThe conversation ID.
Exceptions
HyphenateExceptionThe possible exceptions are as follows: EMError#USER_NOT_LOGIN, EMError#SERVER_NOT_REACHABLE, and EMError#MESSAGE_INVALID. See EMError.

◆ ackMessageRead()

void com.hyphenate.chat.EMChatManager.ackMessageRead ( String  to,
String  messageId 
) throws HyphenateException

Sends the read receipt for a message to the server.

Note

This method applies to one-to-one chats only.

This method only takes effect if you set EMOptions#setRequireAck(boolean) as true.

We recommend that you call ackConversationRead(String) when opening the chat page, and call this method in other cases to reduce the number of method calls.

Parameters
toThe message for which the read receipt is to be sent.
messageIdThe message ID.
Exceptions
HyphenateExceptionA description of the exception, see EMError.

◆ addConversationListener()

void com.hyphenate.chat.EMChatManager.addConversationListener ( EMConversationListener  listener)

Adds the conversation listener.

After a conversation listener is added, you can listen for conversation changes and conversation read receipts.

You can call the removeConversationListener(EMConversationListener) method to remove the conversation listener when it is not required.

Parameters
listenerThe conversation listener to add. See EMConversationListener.

◆ addMessageListener()

void com.hyphenate.chat.EMChatManager.addMessageListener ( EMMessageListener  listener)

Adds the message listener.

After a message listener is added, you can listen for new messages when they arrive.

You can call removeMessageListener(EMMessageListener) to remove the message listener when it is not required.

Parameters
listenerThe message listener to add. See EMMessageListener.

◆ asyncFetchConversationsFromServer()

void com.hyphenate.chat.EMChatManager.asyncFetchConversationsFromServer ( final int  limit,
final String  cursor,
final EMValueCallBack< EMCursorResult< EMConversation > >  callback 
)

Gets the list of conversations from the server with pagination.

The SDK retrieves the list of conversations in the reverse chronological order of their active time (generally the timestamp of the last message).

If there is no message in the conversation, the SDK retrieves the list of conversations in the reverse chronological order of their creation time.

Parameters
limitThe number of conversations that you expect to get on each page. The value range is [1,50].
cursorThe position from which to start to get data. If you pass in null or an empty string (""), the SDK retrieves conversations from the latest active one.
callbackThe query result EMCursorResult, including the cursor for getting data next time and the conversation list. For the last page, the return value of cursor is an empty string.

◆ asyncFetchHistoryMessages()

void com.hyphenate.chat.EMChatManager.asyncFetchHistoryMessages ( String  conversationId,
EMConversationType  type,
int  pageSize,
String  cursor,
EMFetchMessageOption  option,
final EMValueCallBack< EMCursorResult< EMMessage > >  callBack 
)

/**

Uses the pagination to get the messages in the conversation from the server according to the message pulling parameter configuration class (EMFetchMessageOption).

This is a asynchronous method.

Parameters
conversationIdThe conversation ID.
  • One-to-one chat: The ID of the peer user;
  • Group chat: The group ID.
typeThe conversation type. See EMConversationType.
pageSizeThe number of messages you expect to get on each page. The value range is [1,50].
cursorThe cursor position from which to start querying data.
optionThe parameter configuration class for querying historical messages. See EMFetchMessageOption.
Returns
The list of retrieved messages and the cursor for the next query.
Parameters
callBackThe result callback:

◆ asyncFetchPinnedConversationsFromServer()

void com.hyphenate.chat.EMChatManager.asyncFetchPinnedConversationsFromServer ( final int  limit,
final String  cursor,
final EMValueCallBack< EMCursorResult< EMConversation > >  callback 
)

Get the list of pinned conversations from the server with pagination.

The SDK returns pinned conversations in the reverse chronological order of their pinning.

Parameters
limitThe number of conversations that you expect to get on each page. The value range is [1,50].
cursorThe position from which to start getting data. If you pass in null or an empty string (""), the SDK retrieves the pinned conversations from the latest pinned one.
callbackThe query result EMCursorResult, including the cursor for getting data next time and the list of pinned conversations. For the last page, the return value of cursor is an empty string.

◆ asyncPinConversation()

void com.hyphenate.chat.EMChatManager.asyncPinConversation ( final String  conversationId,
boolean  isPinned,
final EMCallBack  callback 
)

Sets whether to pin a conversation.

Parameters
conversationIdThe conversation ID.
isPinnedWhether to pin the conversation:
  • true:Yes.
  • false: No. The conversation is unpinned.
    Parameters
    callbackThe callback for setting whether to pin the conversation.

◆ asyncRecallMessage()

void com.hyphenate.chat.EMChatManager.asyncRecallMessage ( final EMMessage  message,
final EMCallBack  callback 
)

Recalls the sent message.

This is an asynchronous method.

Parameters
messageThe message object.
callbackA object of the EMCallBack class. See EMCallBack.

◆ deleteConversation()

boolean com.hyphenate.chat.EMChatManager.deleteConversation ( String  username,
boolean  deleteMessages 
)

Deletes a conversation and its local messages from the local database.

Parameters
usernameThe conversation ID.
deleteMessagesWhether to delete the historical messages with the conversation.
  • true: Yes.
  • false: No.
Returns
Whether the conversation is successfully deleted.
  • true: Yes.
  • false: No.

◆ deleteConversationFromServer()

void com.hyphenate.chat.EMChatManager.deleteConversationFromServer ( String  username,
EMConversationType  type,
boolean  isDeleteServerMessages,
EMCallBack  deleteCallBack 
)

Deletes the specified conversation and its historical messages from the server.

This is an asynchronous method.

Parameters
usernameThe conversation ID.
typeThe conversation type. See EMConversationType.
isDeleteServerMessagesWhether to delete the historical messages with the conversation.
  • true: Yes
  • false: No
deleteCallBackThe callback for the deletion of the conversation and its historical messages.
  • true: Success;
  • false: Failure.

◆ downloadAttachment()

void com.hyphenate.chat.EMChatManager.downloadAttachment ( final EMMessage  msg)

Downloads the message attachment.

You can also call this method if the attachment fails to be downloaded automatically.

Parameters
msgThe ID of the message with the attachment to be downloaded.

◆ downloadThumbnail()

void com.hyphenate.chat.EMChatManager.downloadThumbnail ( final EMMessage  msg)

Downloads the message thumbnail.

Parameters
msgThe message with the thumbnail to be downloaded. Only the image messages and video messages have a thumbnail.

◆ getAllConversations()

Map< String, EMConversation > com.hyphenate.chat.EMChatManager.getAllConversations ( )

Gets all local conversations.

Conversations will be first retrieved from the memory. If no conversation is found, the SDK retrieves from the local database.

Returns
The retrieved conversations.

◆ getConversation() [1/4]

EMConversation com.hyphenate.chat.EMChatManager.getConversation ( String  id)

Gets the conversation object by conversation ID.

The SDK will return null if the conversation is not found.

Parameters
idThe conversation ID.
Returns
The conversation with the specified ID. The SDK returns null if the conversation is not found.

◆ getConversation() [2/4]

EMConversation com.hyphenate.chat.EMChatManager.getConversation ( String  id,
EMConversationType  type 
)

Gets the conversation by conversation ID and type.

The SDK will return null if the conversation is not found.

Parameters
idThe conversation ID:
  • One-to-one chat: The ID of the peer user;
  • Group chat: The group ID;
  • Chat room chat: The chat room ID;
  • Message thread: The message thread ID.
typeThe conversation type. See EMConversationType
Returns
The conversation object found by ID and type. The SDK returns null if the conversation is not found.

◆ getConversation() [3/4]

EMConversation com.hyphenate.chat.EMChatManager.getConversation ( String  username,
EMConversationType  type,
boolean  createIfNotExists 
)

Gets the conversation object by user ID or group ID and conversation type.

Parameters
usernameThe conversation ID:
  • One-to-one chat: The ID of the peer user;
  • Group chat: The group ID;
  • Chat room chat: The chat room ID;
  • Message thread: The message thread ID.
typeThe conversation type. See EMConversationType.
createIfNotExistsWhether to create a conversation if the specified conversation is not found:
  • true: Yes;
  • false: No.
Returns
The retrieved conversation object. The SDK returns null if the conversation is not found.

◆ getConversation() [4/4]

EMConversation com.hyphenate.chat.EMChatManager.getConversation ( String  username,
EMConversationType  type,
boolean  createIfNotExists,
boolean  isChatThread 
)

Get conversation object by conversation id and conversation type.

If the conversation is not found, you can create a new object based on the value of CreateIFNotExists or an empty object.

Parameters
usernameThe conversation ID.
  • - One-to-one chat: The ID of the peer user;
    • Group chat: The group ID;
    • Chat room chat: The chat room ID;
    • Message thread: The message thread ID.
typeThe conversation type. See EMConversationType.
createIfNotExistsWhether to create a conversation if the specified conversation is not found:
  • true: Yes;
  • false: No.
isChatThreadWhether to search for the chat thread conversation.
  • true: Yes;
    • false: No.
Returns
The retrieved conversation object. The SDK returns null if the conversation is not found.

◆ getConversationsByType()

List< EMConversation > com.hyphenate.chat.EMChatManager.getConversationsByType ( EMConversationType  type)

Gets the list of conversations by conversation type.

Parameters
typeThe conversation type. See EMConversationType.
Returns
The list of conversations of the specified type.

◆ getMessage()

EMMessage com.hyphenate.chat.EMChatManager.getMessage ( String  messageId)

Gets the message by message ID.

Parameters
messageIdThe message ID.
Returns
The message object with the specified ID. The SDK returns null if the message does not exist.

◆ getUnreadMessageCount()

int com.hyphenate.chat.EMChatManager.getUnreadMessageCount ( )

Gets the unread message count.

Returns
The count of unread messages.

◆ recallMessage()

void com.hyphenate.chat.EMChatManager.recallMessage ( EMMessage  message) throws HyphenateException

Recalls the sent message.

If the recipient is offline when the message is delivered and recalled, the recipient only receives the callback EMMessageListener#onMessageRecalled instead of the message.

This is a synchronous method and blocks the current thread.

Parameters
messageThe message instance.
Exceptions
HyphenateExceptionA description of the exception. See EMError.

◆ removeConversationListener()

void com.hyphenate.chat.EMChatManager.removeConversationListener ( EMConversationListener  listener)

Removes the conversation listener.

After adding a conversation listener with addConversationListener(EMConversationListener), you can call this method to remove it when it is not required.

Parameters
listenerThe conversation listener to remove.

◆ removeMessageListener()

void com.hyphenate.chat.EMChatManager.removeMessageListener ( EMMessageListener  listener)

Removes the message listener.

After adding a message listener with addMessageListener(EMMessageListener), you can call this method to remove it when it is not required.

Parameters
listenerThe message listener to remove.

◆ removeMessagesFromServer()

void com.hyphenate.chat.EMChatManager.removeMessagesFromServer ( String  conversationId,
EMConversationType  type,
long  beforeTimeStamp,
EMCallBack  callBack 
)
protected

Retrieves messages of a certain type in the conversation from the local database.

Parameters
typeThe message type. See Type.
timeStampThe starting Unix timestamp in the message for query. The unit is millisecond. After this parameter is set, the SDK retrieves messages, starting from the specified one, according to the message search direction. If you set this parameter as a negative value, the SDK retrieves messages, starting from the current time, in the descending order of the timestamp included in them.
maxCountThe maximum number of messages to retrieve each time. The value range is [1,400].
fromThe user ID of the message sender in one-to-one chat or group chat. If this parameter is set to null or an empty string, the SDK searches for messages in the entire conversation.
directionThe message search direction. See EMConversation.EMSearchDirection.
  • (Default) UP: The SDK retrieves messages in the descending order of the timestamp included in them.
    • DOWN: The SDK retrieves messages in the ascending order of the timestamp included in them.
Returns
The list of retrieved messages (excluding the one with the starting timestamp). If no message is obtained, an empty list is returned.

Retrieves messages with keywords in the conversation in the local database.

Parameters
keywordsThe keywords for query.
timeStampThe starting Unix timestamp in the message for query. The unit is millisecond. After this parameter is set, the SDK retrieves messages, starting from the specified one, according to the message search direction. If you set this parameter as a negative value, the SDK retrieves messages, starting from the current time, in the descending order of the timestamp included in them.
maxCountThe maximum number of messages to retrieve each time. The value range is [1,400].
fromThe user ID of the message sender in one-to-one chat or group chat. If this parameter is set to null or an empty string, the SDK searches for messages in the entire conversation.
directionThe message search direction. See EMConversation.EMSearchDirection.
  • (Default) UP: The SDK retrieves messages in the descending order of the timestamp included in them.
    • DOWN: The SDK retrieves messages in the ascending order of the timestamp included in them.
Returns
The list of retrieved messages (excluding the one with the starting timestamp). If no message is obtained, an empty list is returned.

Deletes local historical messages with a Unix timestamp before a specified one.

Parameters
timeStampThe starting Unix timestamp in the message for query. This unit is millisecond.
callbackThe result callback. See EMCallBack.

Reports an inappropriate message.

This is an asynchronous method.

Parameters
msgIdThe ID of the message to report.
tagThe tag of the inappropriate message. You need to type a custom tag, like porn or ad.
reportReasonThe reporting reason. You need to type a specific reason.
callBackThe result callback. See EMCallBack.

Reports an inappropriate message.

This is a synchronous method and blocks the current thread.

Parameters
msgIdThe ID of the inappropriate message.
tagThe tag of the inappropriate message. You need to type a custom tag, like porn or ad.
reportReasonThe reporting reason. You need to type a specific reason.
Exceptions
HyphenateExceptionA description of the exception. See EMError. 获取翻译服务支持的语言。
Parameters
callBack结果回调,详见 EMCallBack

Gets all languages supported by the translation service.

Parameters
callBackThe result callback. See EMCallBack.

Translates a text message.

Parameters
messageThe message object for translation.
languagesThe list of target language codes.
callBackThe result callback. See EMCallBack.

Adds a Reaction.

This is a synchronous method.

Parameters
messageIdThe message ID.
reactionThe Reaction content.
Exceptions
HyphenateExceptionA description of the exception. See EMError.

Adds a Reaction.

This is an asynchronous method.

Parameters
messageIdThe message ID.
reactionThe message Reaction to add.
callbackThe result callback which contains the error information if the method fails. See EMCallBack.

Deletes a Reaction.

This is a synchronous method.

Parameters
messageIdThe message ID.
reactionThe Reaction content to delete.
Exceptions
HyphenateExceptionA description of the exception. See EMError.

Deletes a Reaction.

This is an asynchronous method.

Parameters
messageIdThe message ID.
reactionThe Reaction to delete.
callbackThe result callback which contains the error information if the method fails. See EMCallBack.

Gets the list of Reactions.

This is a synchronous method.

Parameters
messageIdListThe message ID.
chatTypeThe conversation type. Only the one-to-one chats (EMMessage.ChatType#Chat and group chats (EMMessage.ChatType#GroupChat) are allowed.
groupIdThe group ID, which is invalid only for group chats.
Returns
map The Reaction list under the specified message ID. The UserList of EMMessageReaction indicates the summary data, which only contains the information of the first three users.
Exceptions
HyphenateExceptionA description of the exception. See EMError.

Gets the list of Reactions.

This is an asynchronous method.

Parameters
messageIdListThe message ID.
chatTypeThe conversation type. Only one-to-one chats (EMMessage.ChatType#Chat and group chats (EMMessage.ChatType#GroupChat) are allowed.
groupIdThe group ID, which is valid only for group chats.
callbackThe Reaction list under the specified message ID. The UserList of EMMessageReaction indicates the summary data, which only contains the information of the first three users.

Gets the Reaction details.

This is a synchronous method.

Parameters
messageIdThe message ID.
reactionThe Reaction content.
cursorThe cursor position from which to start querying data.
pageSizeThe number of Reactions you expect to get on each page.
Returns
The query result EMCursorResult, which contains the list of obtained Reactions and the cursor for the next query. If the cursor is null, all data is obtained.
Exceptions
HyphenateExceptionA description of the exception, see EMError.

Gets the Reaction details.

This is an asynchronous method.

Parameters
messageIdThe message ID.
reactionThe Reaction content.
cursorThe cursor position from which to start querying data.
pageSizeThe number of Reactions you expect to get on each page.
callbackThe query result EMCursorResult, which contains the cursor for the next query and EMMessageReaction list (only the first data entry is used).

Unidirectionally removes historical message by message ID from the server.

Parameters
conversationIdThe conversation ID.
typeThe conversation type.
msgIdListThe list of IDs of messages to be removed.
callBackThe result callback which contains the error information if the method fails. See EMCallBack.

Unidirectionally removes historical message by timestamp from the server.

Parameters
conversationIdThe conversation ID.
typeThe conversation type.
beforeTimeStampThe UNIX timestamp in millisecond. Messages with a timestamp smaller than the specified one will be removed.
callBackThe result callback which contains the error information if the method fails. See EMCallBack.

◆ saveMessage()

void com.hyphenate.chat.EMChatManager.saveMessage ( EMMessage  message)

Saves the message to the memory and local database.

The command messages (type: Type#CMD, message body: EMCmdMessageBody) are not saved locally.

Parameters
messageThe message to store.

◆ sendMessage()

void com.hyphenate.chat.EMChatManager.sendMessage ( final EMMessage  msg)

Sends a message。

For a voice or image message or a message with an attachment, the SDK will automatically upload the attachment.

You can determine whether to upload the attachment to the chat sever by setting EMOptions#setAutoTransferMessageAttachments(boolean).

To listen for the message sending status, call EMMessage#setMessageStatusCallback(EMCallBack).

Parameters
msgThe message object to be sent. Make sure to set the param.

◆ setVoiceMessageListened()

void com.hyphenate.chat.EMChatManager.setVoiceMessageListened ( EMMessage  message)

Marks a voice message as listened.

Parameters
messageThe message object.

The documentation for this class was generated from the following file: