Skip to main content

Insert Messages

The message insertion API does not send messages to the remote server, so inserted messages will not sync to the local database when reinstalling the app or logging in from another device.

Insert Outgoing Messages

Use the insertOutgoingMessage: method to insert an outgoing message into the local database. The following example uses the insertOutgoingMessage method from RCChannelClient.

tip
  • Incorrect sentTime may affect message sorting—use with caution!
  • Inserted messages won’t immediately appear in the chat UI.

Method Signature

- (void)insertOutgoingMessage:(RCConversationType)conversationType
targetId:(NSString *)targetId
sentStatus:(RCSentStatus)sentStatus
content:(RCMessageContent *)content
sentTime:(long long)sentTime
completion:(nullable void(^)(RCMessage *_Nullable message))completion;

To enable message extensions for locally inserted messages, use the insertion method from RCChannelClient. Otherwise, the canIncludeExpansion property remains unavailable. Note that extension data cannot be set during insertion. You may set it later as needed. For details, see [Message Extensions].

Parameters

ParameterTypeDescription
conversationType[RCConversationType]Conversation type.
targetIdNSStringTarget ID of the conversation.
channelIdNSStringChannel ID for ultra group conversations. Pass nil for non-ultra groups.
canIncludeExpansionBOOLWhether the message supports extensions. YES: enabled; NO: disabled.
sentStatus[RCSentStatus]Message delivery status.
content[RCMessageContent]Message content.
sentTimelong longUnix timestamp (ms) of the message. Pass 0 to use the local time.
completionBlockAsync callback returning the inserted [RCMessage] object.

Example

RCTextMessage *content = [RCTextMessage messageWithContent:@"Test message"];

[[RCChannelClient sharedChannelManager]
insertOutgoingMessage:ConversationType_PRIVATE
targetId:@"targetId"
channelId:@"channelId"
canIncludeExpansion:YES
sentStatus:SentStatus_SENT
content:content
sentTime:sentTime
completion::^(RCMessage * _Nullable message) {

}];

Insert Incoming Messages

Use the insertIncomingMessage: method to insert an incoming message into the local database.

tip
  • Incorrect sentTime may affect message sorting—use with caution!
  • For versions after 5.6.8, use RCReceivedStatusInfo to set message receipt status. Earlier versions use RCReceivedStatus.
  • Inserted messages won’t immediately appear in the chat UI.

Method Signature

- (void)insertIncomingMessage:(RCConversationType)conversationType
targetId:(NSString *)targetId
senderUserId:(NSString *)senderUserId
receivedStatusInfo:(RCReceivedStatusInfo *)receivedStatusInfo
content:(RCMessageContent *)content
sentTime:(long long)sentTime
completion:(nullable void(^)(RCMessage *_Nullable message))completion;

Parameters

ParameterTypeDescription
conversationType[RCConversationType]Conversation type. Use ConversationType_PRIVATE for one-to-one chats.
targetIdNSStringUnique identifier for the conversation (Target ID or conversation ID). Note: For one-to-one chats, the Target ID is always the peer user's ID. For groups and ultra groups, it’s the group ID or ultra group ID. See Message Introduction for details.
senderUserIdNSStringSender’s user ID.
receivedStatusInfoRCReceivedStatusInfoMessage receipt status.
content[RCMessageContent]Message content.
sentTimelong longUnix timestamp (ms) of the message. Pass 0 to use the local time.
completionBlockAsync callback returning the inserted [RCMessage] object.

Example

RCTextMessage *content = [RCTextMessage messageWithContent:@"Test message"];
RCReceivedStatusInfo *info = [[RCReceivedStatusInfo alloc] initWithReceivedStatus:0];
[[RCCoreClient sharedCoreClient] insertIncomingMessage:ConversationType_PRIVATE
targetId:@"targetId"
senderUserId:@"senderUserId"
receivedStatusInfo:info
content:content
sentTime:0
completion:^(RCMessage * _Nullable message) {

}];

Display Inserted Messages in Chat UI

To show inserted messages immediately in RCConversationViewController, call appendAndDisplayMessage: with the RCMessage object returned by the insertion method.

RCMessage *insertMessage;
[self appendAndDisplayMessage:insertMessage];