Browse SDKs · HarmonyOS
Platform
SDKsHarmonyOSEnterprise

Pin or unpin message

Pin or unpin a message within a conversation.

Copy

Use pinned to toggle the pinned status of a message.

Code example

import sdk, {
  EventOnChangedPinnedMsgData,
  OpenIMSDKEvent,
  SetConversationPinnedMsgReq,
} from '@openimsdk/imsdk';

const request: SetConversationPinnedMsgReq = {
  conversationID: conversationID,
  clientMsgID: message.clientMsgID,
  pinned: true,
};
await sdk.setConversationPinnedMsg(request);

const handlePinnedMessagesChanged = (data: EventOnChangedPinnedMsgData): void => {
  replacePinnedMessageRecords(data.conversationID, data.msgs);
};
const unsubscribePinnedMessages = sdk.on(
  OpenIMSDKEvent.EventOnChangedPinnedMsg,
  handlePinnedMessagesChanged,
);

export function removePinnedMessageListener(): void {
  unsubscribePinnedMessages();
}

Parameters

ParameterTypeRequiredDescription
conversationIDstringYesTarget conversation identifier.
clientMsgIDstringYesClient message identifier of the target message.
pinnedbooleanYesPass true to pin the message; pass false to unpin it.

Return value

Returns a Promise that resolves to void upon server acceptance.

State changes after the call

Permissions, eligible message types, and maximum pin count limits are verified by OpenIMServer. Resolving the Promise confirms server processing, but does not indicate that the change event has arrived. The msgs property in the event delivers records containing pinner identity, timestamps, and message details. Clients reconcile pinned states using conversationID and clientMsgID. To load initial pinned messages, see Get pinned messages in conversation. Remove listeners when disposing of the state layer.