Browse SDKs · Android
Platform
SDKsAndroid

Create a custom message

Use createCustomMessage to carry application-defined JSON content.

Copy

Construct a custom message object carrying application-specific business payloads. By passing structured JSON data, optional extension metadata, and a fallback preview description, you can build custom interactive components such as red packets, polls, business cards, and order updates.

Code example

String dataJson = "{\"action\":\"red_packet\",\"packetID\":\"rp_888\",\"amount\":100}";
String extensionJson = "{\"theme\":\"festive\"}";
String description = "[Received a red packet]";

Message message = OpenIMClient.getInstance().messageManager.createCustomMessage(
    dataJson,
    extensionJson,
    description
);

Parameters

ParameterTypeRequiredDescription
dataStringYesThe primary custom payload, typically a serialized JSON string.
extensionStringNoAdditional business extension metadata. Pass an empty string if not needed.
descriptionStringYesSummary preview text displayed in the conversation list or push notifications.

Return value

Returns the created Message object with contentType set to MessageType.CUSTOM.

State changes after the call

  1. Local initialization: Initializes the local Message structure without sending data to the server.
  2. Schema agreement: Senders and receivers should negotiate schema conventions and implement graceful degradation for unrecognized versions.
  3. Transmission: Call Send a message to deliver the message.