sendSchedulerMessage static method
Future<SchedulerMessage?>
sendSchedulerMessage(
- SchedulerMessage message, {
- dynamic onSuccess()?,
- dynamic onError()?,
SchedulerMessage used to send a custom message
Implementation
static Future<SchedulerMessage?> sendSchedulerMessage(
SchedulerMessage message, {
dynamic Function(SchedulerMessage)? onSuccess,
dynamic Function(CometChatException)? onError,
}) 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);
SchedulerMessage? result = await SDKMethods.sendSchedulerMessage(message,
onSuccess: (SchedulerMessage sentMessage) {
//executing the custom onSuccess handler
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;
}