Insert a Message
Feature Description
When a message is inserted, the message will only be inserted into the local database but not be sent to the server.
This API is used to insert tips into a conversation, such as "You have left the group" and "Keep your information secure. Do not send private information such as account, password, and verification code to the group chat". Such messages need to be displayed in the chat area, but do not need to be sent to others.
Caution
Inserted messages will be lost if the account is logged in on another mobile device or the application is uninstalled and reinstalled.
Inserting a local message into one-to-one messages
Call
insertC2CMessageToLocalStorage
(Android / iOS and macOS / Windows) to insert a local message into one-to-one messages.Sample code:
// Create a messageV2TIMMessage msg = V2TIMManager.getMessageManager().createTextMessage("Insert a local message between one-to-one messages");// Insert the message into the local databaseV2TIMManager.getMessageManager().insertC2CMessageToLocalStorage(msg, "receiver_userID", "sender_userID", new V2TIMValueCallback<V2TIMMessage>() {@Overridepublic void onSuccess(V2TIMMessage message) {// Inserted the message into one-to-one messages successfully}@Overridepublic void onError(int code, String desc) {// Failed to insert the message into one-to-one messages}});
// Create a messageV2TIMMessage *msg = [[V2TIMManager sharedInstance] createTextMessage:@"Insert a local message between C2C messages"];// Insert the message into the local database[[V2TIMManager sharedInstance] insertC2CMessageToLocalStorage:msgto:@"receiver_userID" // `userID` of the receiversender:@"sender_userID" // `userID` of the sendersucc:^{NSLog(@"Message inserted between one-to-one messages successfully");} fail:^(int code, NSString *msg) {NSLog(@"Failed to insert the message between one-to-one messages, code: %d, msg: %@", code, msg);}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMMessage message =V2TIMManager::GetInstance()->GetMessageManager()->CreateTextMessage(u8"Insert a local message between one-to-one messages");V2TIMString userID = "receiver_userID";V2TIMString sender = "sender_userID";auto callback = new ValueCallback<V2TIMMessage>{};callback->SetCallback([=](const V2TIMMessage& message) {// Inserted the message between one-to-one messages successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to insert the message between one-to-one messagesdelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->InsertGroupMessageToLocalStorage(message, userID, sender,callback);
Inserting a local message into group messages
Call
insertGroupMessageToLocalStorage
(Android / iOS and macOS / Windows) to insert a local message into group messages.Sample code:
// Create a messageV2TIMMessage msg = V2TIMManager.getMessageManager().createTextMessage("Insert a local message between group messages");// Insert the message into the local databaseV2TIMManager.getMessageManager().insertGroupMessageToLocalStorage(msg, "groupID", "sender_userID", new V2TIMValueCallback<V2TIMMessage>() {@Overridepublic void onSuccess(V2TIMMessage message) {// Message inserted into group messages successfully}@Overridepublic void onError(int code, String desc) {// Failed to insert the message into group messages}});
// Create a messageV2TIMMessage *msg = [[V2TIMManager sharedInstance] createTextMessage:@"Insert a local message between group messages"];// Insert the message into the local database[[V2TIMManager sharedInstance] insertGroupMessageToLocalStorage:msgto:@"groupID" // `groupID` of the group chatsender:@"sender_userID" // `userID` of the sendersucc:^{NSLog(@"Message inserted between group messages successfully");} fail:^(int code, NSString *msg) {NSLog(@"Failed to insert the message between group messages, code: %d, msg: %@", code, msg);}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMMessage message =V2TIMManager::GetInstance()->GetMessageManager()->CreateTextMessage(u8"Insert a local message between group messages");V2TIMString groupID = "groupID";V2TIMString sender = "sender_userID";auto callback = new ValueCallback<V2TIMMessage>{};callback->SetCallback([=](const V2TIMMessage& message) {// Message inserted between group messages successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to insert the message between group messagesdelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->InsertGroupMessageToLocalStorage(message, groupID, sender,callback);