sendMediaMessage static method
Future<MediaMessage?>
sendMediaMessage(
- MediaMessage message, {
- dynamic onSuccess()?,
- dynamic onError()?,
- bool replacePathForIOS = true,
sendMediaMessage used to send a media message
Implementation
static Future<MediaMessage?> sendMediaMessage(MediaMessage message,
{dynamic Function(MediaMessage)? onSuccess,
dynamic Function(CometChatException)? onError,
bool replacePathForIOS = true}) async {
if (message.parentMessageId == -1) {
message.parentMessageId = 0;
}
message.sender ??= loggedInUser;
if (message.muid.trim().isEmpty) {
message.muid = DateTime.now().microsecondsSinceEpoch.toString();
}
CometChatMessageEvents.ccMessageSent(message, MessageStatus.inProgress);
MediaMessage? mediaMessage2;
if (replacePathForIOS == true) {
//for sending files
mediaMessage2 = MediaMessage(
receiverType: message.receiverType,
type: message.type,
receiverUid: message.receiverUid,
file: (Platform.isIOS &&
message.file != null &&
(!message.file!.startsWith('file://')))
? 'file://${message.file}'
: message.file,
metadata: message.metadata,
sender: message.sender,
parentMessageId: message.parentMessageId,
muid: message.muid,
category: message.category,
attachment: message.attachment,
caption: message.caption,
tags: message.tags,
);
}
MediaMessage? result = await CometChat.sendMediaMessage(
mediaMessage2 ?? message, onSuccess: (MediaMessage sentMessage) {
//executing the custom onSuccess handler
if (replacePathForIOS == true) {
if (Platform.isIOS) {
if (message.file != null) {
sentMessage.file = message.file?.replaceAll("file://", '');
}
} else {
sentMessage.file = message.file;
}
}
if (onSuccess != null) {
try {
onSuccess(sentMessage);
} catch (e) {
if (kDebugMode) {
debugPrint(
"message sent successfully but failed to execute onSuccess callback");
}
}
}
//the ccMessageSent event is emitted to update the message receipt shown
//in the footer of the message bubble from in progress to sent
CometChatMessageEvents.ccMessageSent(sentMessage, MessageStatus.sent);
}, onError: (error) {
//executing the custom onError handler
if (onError != null) {
try {
onError(error);
} catch (e) {
if (kDebugMode) {
debugPrint(
"message could not be sent and failed to execute onError callback");
}
}
}
//a error property is added to the metadata of the message
//because of which a error message receipt will be shown in the
//footer of the message bubble in the message list
if (message.metadata != null) {
message.metadata!["error"] = error;
} else {
message.metadata = {"error": error};
}
CometChatMessageEvents.ccMessageSent(message, MessageStatus.error);
});
return result;
}