Through these three steps, the custom regular message is complete. When receiving a message, if the type in WKMsg is 3, it indicates that the message is a business card message, where messageContent is the custom GifContent. At this time, you can cast messageContent to GifContent and render it on the UI.Complete code as follows:
Copy
class GifContent extends WKMessageContent { int width = 0; // Width int height = 0; // Height String url; // Remote URL GifContent(this.url) { // Specify message type contentType = WkMessageContentType.gif; } @override WKMessageContent decodeJson(Map<String, dynamic> json) { url = readString(json, 'url'); width = readInt(json, 'width'); height = readInt(json, 'height'); return this; } @override Map<String, dynamic> encodeJson() { return {'url': url, 'width': width, 'height': height}; } // Override if you need to get displayable content @override String displayText() { return "[Animated Image]"; }}
Sometimes we need to send messages with attachments when sending messages. WuKongIM also provides custom attachment messages, which are not much different from regular messages. Below we use location messages as an example.
Note that custom attachment messages need to inherit from WKMediaMessageContent instead of WKMessageContent.
Copy
class WKLocationContent extends WKMediaMessageContent { var longitude = 0.0; var latitude = 0.0; var address = ""; WKLocationContent() { contentType = 10; }}
As business develops, applications have increasingly more features in chat. To meet most requirements, WuKongIM has added message extension functionality. Message extensions are divided into local extensions and remote extensions. Local extensions are only for local app use and will be lost after uninstalling the app. Remote extensions are saved on the server and data will be restored after uninstalling and reinstalling.
When a logged-in user views messages sent by others, if the sender has enabled message receipts, the viewed messages need to be uploaded to the server to mark them as read. When the sender or yourself uploads read messages, the server will send a sync message extension cmd (command) message. At this time, you need to sync the latest message extensions through WKIM.shared.messageManager.saveRemoteExtraMsg(List<WKSyncExtraMsg> list) method and save them to the SDK.
In chat, if there are too many messages, sending message replies will make the messages very messy and hard to follow. At this time, you need to make specific replies to certain messages, which is message reply.When sending a message, you just need to assign the WKReply object in the message content WKMessageContent to achieve the message reply effect.
When you or others react to messages (like), it will trigger cmd (command) message notifications to the application. When the app receives a sync message reaction cmd, it can call the server sync interface to update the obtained reaction data to the SDK.
Copy
// Save message reactionsWKIM.shared.messageManager.saveMessageReactions(List<WKSyncMsgReaction> list);
The same user can only make one reaction to the same message. Repeated reactions with different emojis to the same message will be treated as modifying the reaction, while repeated reactions with the same emoji will be treated as deleting the reaction. After the SDK updates message reactions, it will trigger a message refresh event. The app needs to listen for this event and refresh the UI.
WKIM.shared.messageManager.updateMsgEdit(String messageID, String channelID, int channelType, String content);
After changing the SDK message edit content, you need to upload the edited content to the server, which requires listening for upload message extensions.
WKIM.shared.messageManager.addOnUploadMsgExtra((wkMsgExtra) => { // Upload to your own server });
If you or others edit messages, it will trigger cmd (command) messages. The app can determine based on the cmd type and then sync message extensions. The app needs to listen for message update events to complete UI refresh.