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.Listen for message storage events:
Copy
WKIM.shared.messageManager.addOnMsgInsertedListener((wkMsg) { // Display in UI });
// Listen for new message eventsWKIM.shared.messageManager.addOnNewMsgListener('chat', (msgs) { // Display in UI });// Remove new message listenerWKIM.shared.messageManager.removeNewMsgListener('chat');
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.Listen for refresh message events:
Copy
// Listen for refresh message eventsWKIM.shared.messageManager.addOnRefreshMsgListener('chat', (wkMsg) { // TODO refresh message });// Remove refresh message listenerWKIM.shared.messageManager.removeOnRefreshMsgListener('chat');
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 pullMode Pull mode 0: pull down 1: pull up * @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 * @param syncBack Sync message callback, can show loading through this callback */WKIM.shared.messageManager.getOrSyncHistoryMessages( channelID, channelType, oldestOrderSeq, contain, pullMode, limit, aroundMsgOrderSeq, Function(List<WKMsg>)){ }, Function() syncBack);
Getting history messages is not a synchronous method, as there may be non-continuous data that needs to be synced from the server
Need to implement sync channel message data sourceChannel Message Data SourceBecause WuKongIM supports permanent message storage, it will generate massive offline messages. For this, we adopt an on-demand pull mechanism. For example, with 10 conversations each having 100,000 messages, WuKongIM will not pull all 10*100,000=1 million messages to local storage. Instead, it pulls information for these 10 conversations and the corresponding latest 20 messages, which means actually only 200 messages are pulled. Compared to 1 million messages, this greatly improves offline pull speed. Users will only pull messages for a specific conversation when they enter that conversation. These mechanisms are already encapsulated within the SDK, so users don’t need to worry about them. Users only need to focus on recent conversation changes and listen for data retrieval callbacks.
class WKMsg { // Message header redDot: whether to show red dot noPersist: whether not to store syncOnce: whether to sync only once MessageHeader header = MessageHeader(); // Message settings receipt: whether receipt, topic: whether topic chat, stream: whether stream message; Setting setting = Setting(); // Server message ID (globally unique, unordered) String messageID = ""; // Server message ID (ordered) int messageSeq = 0; // Local message ordered ID int clientSeq = 0; // 10-digit timestamp int timestamp = 0; // Local unique ID String clientMsgNO = ""; // Sender String fromUID = ""; // Channel ID String channelID = ""; // Channel type int channelType = WKChannelType.personal; // Message content type e.g. 1:[Text] 2:[Image]... int contentType = 0; // Message payload String content = ""; // Message status 0.sending 1.success int status = 0; // Whether deleted 1.yes int isDeleted = 0; // Sender's profile WKChannel? _from; // Channel profile WKChannel? _channelInfo; // Sender's type profile in channel (only for group messages) WKChannelMember? _memberOfFrom; // Sort number int orderSeq = 0; // Local extension fields dynamic localExtraMap; // Remote extension fields, maintained by server WKMsgExtra? wkMsgExtra; // Message reaction data List<WKMsgReaction>? reactionList; // Message content body contentType==1.WKTextContent contentType==2.WKImageContent WKMessageContent? messageContent;}