sendCustomMessage static method

Future<CustomMessage?> sendCustomMessage(
  1. CustomMessage message, {
  2. required dynamic onSuccess(
    1. CustomMessage message
    )?,
  3. required dynamic onError(
    1. CometChatException excep
    )?,
})

send custom messages which are neither text nor media messages.

receiverId Unique id of the user or group to which the message is to be sent.

receiverType Type of the receiver i.e user or group

customType custom message type that you need to set.

customData The data to be passed as the message in the form of a JSONObject.

Migration Note: Migrated from platform channels to native Dart implementation. Behavior and signature remain identical for backward compatibility.

Android Reference: MessagesRequest.sendCustomMessage(CustomMessage message)

The method could throw PlatformException with error codes specifying the cause

Implementation

static Future<CustomMessage?> sendCustomMessage(CustomMessage message,
    {required Function(CustomMessage message)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call native Dart message repository
    final sentMessage = await sdk.messages.sendCustomMessage(message);

    // Call success callback
    if (onSuccess != null) onSuccess(sentMessage);
    return sentMessage;
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
  return null;
}