WuKongIM Android SDK advanced features, including custom messages, message extensions, message receipts and message replies
Advanced features provide developers with the ability to extend WuKongIM Android SDK, including custom message types, message extensions, message receipts, message editing and message replies and other enterprise-level features.
In WuKongIM, all message types are custom messages
Define a message object that inherits from WKMessageContent and specify the message type in the constructor.
Built-in message types in SDK can be viewed through WKMsgContentType
Copy
public class WKCardContent extends WKMessageContent { public WKCardContent() { type = 3; // Specify message type } // Define fields to send to the recipient public String uid; // User ID public String name; // Name public String avatar; // Avatar}
Note: Custom message objects must provide a parameterless constructor
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 baseContentMsgModel is the custom WKCardContent. At this time, you can cast baseContentMsgModel to WKCardContent and render it on the UI.
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
public class WKLocationContent extends WKMediaMessageContent { // Define fields to send to the recipient public double longitude; // Longitude public double latitude; // Latitude public String address; // Detailed address information public WKLocationContent(double longitude, double latitude, String address) { type = 6; this.longitude = longitude; this.latitude = latitude; this.address = address; } // Must provide parameterless constructor here public WKLocationContent() { type = 6; }}
WKMediaMessageContent provides url and localPath fields, so custom messages don’t need to define network address and local address fields again
When encoding messages, you can write localPath local fields. After the SDK saves the message, the message sent to the recipient does not include this field
Override the decodeMsg method of WKMessageContent to start decoding:
When decoding messages, if decoding local fields, you need to check if the field exists, because received messages don’t have local fields. For example, localPath is not available when receiving messages
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.
Remote extensions are the remoteExtra field in the message object WKMsg.
Copy
/** * Save remote extensions * @param channel Channel information * @param list Remote extension data */WKIM.getInstance().getMsgManager().saveRemoteExtraMsg(WKChannel channel, List<WKSyncExtraMsg> list);
After successful update, the SDK will trigger a refresh message callback
Message read/unread is also called message receipts. Message receipt functionality can be set through settings.
Copy
WKMsgSetting setting = new WKMsgSetting();setting.receipt = 1; // Enable receiptsWKSendOptions options = new WKSendOptions();options.setting = setting;// Send messageWKIM.getInstance().getMsgManager().sendWithOptions(contentModel, channel, options);
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 syncMessageExtra. At this time, you need to sync the latest message extensions and save them to the SDK.
/** * Modify edit content * @param msgID Message server ID * @param channelID Channel ID * @param channelType Channel type * @param content Edited content */WKIM.getInstance().getMsgManager().updateMsgEdit(String msgID, String channelID, byte 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.
// Listen for upload message extensionsWKIM.getInstance().getMsgManager().addOnUploadMsgExtraListener(new IUploadMsgExtraListener() { @Override public void onUpload(WKMsgExtra msgExtra) { // Upload to your own server }});
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.
public class WKReply { // Root message ID of the replied message, the first reply message ID in multi-level replies public String root_mid; // Replied message ID public String message_id; // Replied MessageSeq public long message_seq; // Replied user uid public String from_uid; // Replied user name public String from_name; // Replied message body public WKMessageContent payload; // Edited content of replied message public String contentEdit; // Edited message entity of replied message public WKMessageContent contentEditMsgModel; // Edit time public long editAt;}
// Save message reactionsWKIM.getInstance().getMsgManager().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.