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.
- The ZIM SDK currently supports inserting local messages for "peer/group" conversations but does not support inserting local messages for "room" conversations.
- The ZIM SDK does not support inserting signaling messages.
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.
// 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
// 1. Create an In-app Chat object and introduce the appID and appSign.
ZIMAppConfig *appConfig = [[ZIMAppConfig alloc] init];
appConfig.appID = (unsigned int)appID; // Enter the appID you applied for.
appConfig.appSign = @"appSign"; // Enter the appSign you applied for.
self.zim = [ZIM createWithAppConfig: appConfig];
// 2. Log in to the In-app Chat service.
ZIMUserInfo *userInfo = [[ZIMUserInfo alloc]init];
userInfo.userID = @"zegoUser1"; // Enter a value of the NSString type.
userInfo.userName = @"zegotest";// Enter a value of the NSString type.
[self.zim loginWithUserInfo:userInfo callback:^(ZIMError * _Nonnull errorInfo){
// 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.
ZIMCustomMessage *customMessage = [[ZIMCustomMessage alloc] init];
customMessage.message = @"custom";
customMessage.subType = 1; // The custom system message type defined by the business, with values ranging from [0,200].
NSString *conversationID = @"conversationID";
ZIMConversationType type = ZIMConversationTypePeer;
NSString *senderUserID = @"senderUserID";
[self.zim
insertMessageToLocalDB: customMessage
conversationID:conversationID
conversationType:type
senderUserID:senderUserID
callback:^(ZIMMessage *_Nonnull message, ZIMError *_Nonnull errorInfo) {
GGLog(@"errorcode", errorInfo.code);
}];
1
// 1. Create an In-app Chat object and introduce the appID and appSign.
zim::ZIMAppConfig app_config;
app_config.appID = 12345; // Enter the appID you applied for.
app_config.appSign = "appSign"; // Enter the appSign you applied for.
zim_ = zim::ZIM::create(app_config);
// 2. Set the setEventHandler callback
im_event_handler_ = std::make_shared<CZIMEventHandler>();
zim_->setEventHandler(im_event_handler_);
// 3. Login
zim::ZIMUserInfo user_info;
user_info.userID = user_id;
user_info.userName = user_name;
zim_->login(user_info, [=](zim::ZIMError errorInfo){
// Here you can get the login result and execute user code based on the error code
});
// 4. Insert a message into the local database
// Here is an example of inserting a custom message, you can modify it to insert other types of messages, signaling messages are not supported.
std::string conversationID="conversationID";
std::string sender_user_id = "sender_user_id";
zim::ZIMConversationType type = zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER;
int subType = 1; // The system message type defined by the business, with values ranging from [0, 200]
auto customMessage = std::make_shared<zim::ZIMCustomMessage>("custom message", subType);
zim_->insertMessageToLocalDB(std::static_pointer_cast<zim::ZIMMessage>(customMessage), conversationID, type, sender_user_id, [=](const std::shared_ptr<zim::ZIMMessage> &message, const zim::ZIMError &errorInfo) {
// You can listen to the callback of inserting messages here
});
1
// Insert a message into the local database.
// Inserting a text message is used as an example here. You can also insert other types of messages. However, signaling messages are not supported.
const message = { type: 1, message: 'string' };
const conversationType = 0;
zim.insertMessageToLocalDB(
message,
'conversationID',
conversationType,
'senderUserID'
).then((res) => {
// You can listen for whether the insertion is successful.
});
1
// 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
// 1. Create a ZIM object and pass in the appID and appSign
ZIMAppConfig appConfig = new ZIMAppConfig();
appConfig.appID = 12345; // Replace with your assigned AppID
appConfig.appSign = "appSign"; // Replace with your assigned AppSign
ZIM.Create(appConfig);
// 2. Log in
ZIMUserInfo zimUserInfo = new ZIMUserInfo();
zimUserInfo.userID = "xxxx";
zimUserInfo.userName = "xxxx";
ZIM.GetInstance().Login(zimUserInfo, (ZIMError errorInfo) =>
{
// Developers can use ZIMError to determine if the login is successful.
}
);
// 3. Insert a message into the local database
// Here is an example of inserting a custom message, you can modify it to insert other types of messages (signaling messages are not supported).
int subType = 0; // Custom message type defined by the business, with a value between 0 and 200
ZIMCustomMessage customMessage = new ZIMCustomMessage("message", subType);
string conversationID = "conversationID";
string senderUserID = "senderUserID";
ZIMConversationType type = ZIMConversationType.Peer;
ZIM.GetInstance().InsertMessageToLocalDB(customMessage, conversationID, type, senderUserID, (ZIMMessage message, ZIMError errorInfo) =>
{
// You can listen to this callback to check if the message is inserted successfully
});
1