sendMessage static method

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

To send a text message to a single user or group. returns the TextMessage with message id

receiver The UID or GUID of the recipient

messageText The text to be sent

receiverType The type of the receiver to whom the message is to be sent i.e user or group

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

Android Reference: MessagesRequest.sendMessage(TextMessage message)

The method could throw PlatformException with error codes specifying the cause

Implementation

static Future<TextMessage?> sendMessage(TextMessage message,
    {required Function(TextMessage 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.sendTextMessage(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;
}