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 = ZIMAppConfig();
appConfig.appID = 12345;  // Enter the appID you applied for.
appConfig.appSign = "appSign";   // Enter the appSign you applied for.
zim = ZIM.create(appConfig);

// 2. Log in to the In-app Chat service.
try{
    ZIMLoginConfig loginConfig = ZIMLoginConfig();
    // The user's nickname, leave it blank if you don't want to modify the nickname
    loginConfig.userName = 'userName';
    // If using token as the login authentication method, please fill in this parameter, otherwise no need to fill in
    loginConfig.token = '';
    // Whether this login is an offline login, please refer to the offline login documentation for details
    loginConfig.isOfflineLogin = false;
    await ZIM.getInstance()?.login('zego', loginConfig);
    // Login successful, write the business logic for successful login
} on PlatformException catch(onError){
    // Login failed
    // Error code for login failure, please refer to the error code table in the documentation for handling
    onError.code;
    // Error message for login failure
    onError.message;
}



// 3. Insert a message into the local database
// Here we demonstrate inserting a custom message, developers can modify it to insert other types of messages, signaling messages are not supported.
String message = "custom";
int subType = 1; // The 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(message, conversationID, type, senderUserID)
    .then((value) {
})
    .catchError((onError) {
});
1
Copied!

Previous

Delete messages

Next

Forward messages