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 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
Copied!

Previous

Delete messages

Next

Forward messages