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.

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 {
    final result = await channel.invokeMethod('sendCustomMessage', {
      'receiverId': message.receiverUid,
      'receiverType': message.receiverType,
      'customType': message.type,
      'customData': message.customData,
      'subType': message.subType,
      'muid': message.muid,
      'parentMessageId': message.parentMessageId,
      'metadata' :  json.encode(message.metadata ?? {}),
    });
    final res = CustomMessage.fromMap(result);
    if(onSuccess != null) onSuccess(res);
    return res;
  } on PlatformException catch (platformException) {
    if(onError != null) onError(CometChatException(platformException.code, platformException.details, platformException.message));
  } catch (e) {
    debugPrint("Error: $e");
    if(onError != null) onError(CometChatException(ErrorCode.errorUnhandledException, e.toString() , e.toString()));
  }
  return null;
}