logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

Insert local messages


Overview

The ZIM SDK supports inserting local messages into one-on-one and group conversations, and starting from version 2.13, it also supports inserting local messages into room conversations. The inserted messages will only exist locally on the device and will not be sent to other users or synchronized to other devices. Additionally, these messages will not be retained after uninstalling the client application.

You can use this feature to insert a message into the local database for displaying system notifications. For example, it can be used for local notifications such as joining or leaving a group, which do not need to be sent to other users across devices.

Implementation process

To insert a message into the local database, you need to call the insertMessageToLocalDB API and introduce the created ZIMMessage message, conversationID, conversationType, senderUserID, and other parameters.

Sample code
// 1. Create an In-app Chat object and introduce the appID, appSign, and Application in Android.
ZIMAppConfig appConfig = new ZIMAppConfig();
appConfig.appID = 12345;  // Enter the appID you applied for.
appConfig.appSign = "appSign";   // Enter the appSign you applied for.
zim = ZIM.create(appConfig, application);

// 2. Log in to the In-app Chat service.
ZIMUserInfo zimUserInfo = new ZIMUserInfo();
zimUserInfo.userID = "xxxx";
zimUserInfo.userName = "xxxx";
zim.login(zimUserInfo, new ZIMLoggedInCallback() {
    @Override
    public void onLoggedIn(ZIMError error) {
          // You can determine whether the login is successful based on ZIMError.          
    }
 });

// 3. Insert a message into the local database.
// Inserting a custom message is used as an example here. You can also insert other types of messages. However, signaling messages are not supported.
 String message = "custom";
 int subType = 1; // The custom system message type defined by the business, with values ranging from [0,200].
 ZIMCustomMessage customMessage = new ZIMCustomMessage(message,subType);
String conversationID ="conversationID";
String senderUserID = "senderUserID";
ZIMConversationType type = ZIMConversationType.PEER;
zim.insertMessageToLocalDB(customMessage, conversationID, type, senderUserID, new ZIMMessageInsertedCallback() {
    @Override
    public void onMessageInserted(ZIMMessage message, ZIMError errorInfo) {
        // You can listen for whether the insertion is successful through this callback.
    }
});
1
Copied!

Previous

Delete messages

Next

Forward messages