WuKongIM Android SDK message management functionality, including message sending/receiving, history messages and message monitoring
The message manager is responsible for CRUD operations on messages, new message listening, refresh message listening, message storage, send message receipt listening, monitoring sync of specific chat data, etc.
When sending messages, the SDK will trigger a storage callback after saving the message to the local database. At this point, the message has not been sent yet, and you can display the message in the UI in this listener.
Copy
WKIM.getInstance().getMsgManager().addOnSendMsgCallback("key", new ISendMsgCallBackListener() { @Override public void onInsertMsg(WKMsg wkMsg) { // You can display the message `wkMsg` saved in the database on the UI here runOnUiThread(() -> { addMessageToUI(wkMsg); }); }});
For explanation about whether to pass a unique key for events, see Event Listening
// Add listenerWKIM.getInstance().getMsgManager().addOnNewMsgListener("key", new INewMsgListener() { @Override public void newMsg(List<WKMsg> list) { // list: received messages runOnUiThread(() -> { handleNewMessages(list); }); }});// Remove listener when exiting pageWKIM.getInstance().getMsgManager().removeNewMsgListener("key");
If you receive new messages in a chat page, you need to determine whether the message belongs to the current conversation by checking the channelID and channelType of the message object WKMsg
When the SDK updates messages, such as: message send status, someone likes a message, message read receipt, message recall, message editing, etc., the SDK will callback the following event. The UI can determine which specific message has changed through the clientMsgNO of the message object WKMsg.
Copy
// Add refresh listenerWKIM.getInstance().getMsgManager().addOnRefreshMsgListener("key", new IRefreshMsg() { @Override public void onRefresh(WKMsg wkMsg, boolean isEnd) { // wkMsg: refreshed message object // isEnd: to avoid frequent UI refreshes causing lag, refresh UI only when isEnd is true if (isEnd) { runOnUiThread(() -> { refreshMessageInUI(wkMsg); }); } }});// Remove refresh listener when exiting pageWKIM.getInstance().getMsgManager().removeRefreshMsgListener("key");
When a message is sent, you can obtain the WKMsg object by listening for message refresh events. The status (send status) and reasonCode in WKMsg indicate the result of the message delivery.
/** * Query or sync messages for a channel * * @param channelId Channel ID * @param channelType Channel type * @param oldestOrderSeq Last message's large orderSeq, pass 0 for first entry into chat * @param contain Whether to include the oldestOrderSeq message * @param dropDown Whether it's a dropdown * @param aroundMsgOrderSeq Query messages around this message, e.g. aroundMsgOrderSeq=20 returns [16,17,19,20,21,22,23,24,25] * @param limit Number to get each time * @param iGetOrSyncHistoryMsgBack Request callback */WKIM.getInstance().getMsgManager().getOrSyncHistoryMessages( channelId, channelType, oldestOrderSeq, contain, dropDown, limit, aroundMsgOrderSeq, new IGetOrSyncHistoryMsgBack() { @Override public void onSyncing() { // Syncing - show loading as needed } @Override public void onResult(List<WKMsg> list) { // Display messages } });
Getting history messages is not a synchronous method, as there may be non-continuous data that needs to be synced from the server