sendMessageWithCallBack static method

Future<Message?> sendMessageWithCallBack(
  1. int? conversationType,
  2. String? targetId,
  3. MessageContent? content,
  4. String? pushContent,
  5. String? pushData,
  6. dynamic finished(
    1. int messageId,
    2. int status,
    3. int code
    )?, {
  7. bool disableNotification = false,
})

发送消息

conversationType 会话类型,参见枚举 RCConversationType

targetId 会话 id

content 消息内容 参见 MessageContent

finished 回调结果,告知 messageId(消息 id)、status(消息发送状态,参见枚举 RCSentStatus) 和 code(具体的错误码,0 代表成功)

当接收方离线并允许远程推送时,会收到远程推送。 远程推送中包含两部分内容,一是pushContent,用于显示;二是pushData,用于携带不显示的数据。

SDK内置的消息类型,如果您将pushContentpushData置为空或者为null,会使用默认的推送格式进行远程推送。 自定义类型的消息,需要您自己设置pushContent和pushData来定义推送内容,否则将不会进行远程推送。

发送消息之后有两种查看结果的方式:1、发送消息的 callback(消息插入数据库时会走一次 onMessageSend) 2、onMessageSend;推荐使用 callback 的方式 如果未实现此方法的 callback,则会通过 onMessageSend 返回发送消息的结果

Implementation

static Future<Message?> sendMessageWithCallBack(int? conversationType, String? targetId, MessageContent? content, String? pushContent, String? pushData, Function(int messageId, int status, int code)? finished, {bool disableNotification = false}) async {
  if (conversationType == null || targetId == null || content == null) {
    developer.log("send message fail: conversationType or targetId or content is null", name: "RongIMClient");
    return null;
  }
  if (pushContent == null) {
    pushContent = "";
  }
  if (pushData == null) {
    pushData = "";
  }
  String? jsonStr = content.encode();
  String? objName = content.getObjectName();

  // 此处获取当前时间戳传给原生方法,并且当做 sendMessageCallbacks 的 key 记录 finished
  DateTime time = DateTime.now();
  int timestamp = time.millisecondsSinceEpoch;

  Map map = {'conversationType': conversationType, 'targetId': targetId, "content": jsonStr, "objectName": objName, "pushContent": pushContent, "pushData": pushData, "timestamp": timestamp, "disableNotification": disableNotification};

  if (finished != null) {
    sendMessageCallbacks[timestamp] = finished;
  }

  Map? resultMap = await _channel.invokeMethod(RCMethodKey.SendMessage, map);
  if (resultMap == null) {
    return null;
  }
  String? messageString = resultMap["message"];
  Message? msg = MessageFactory.instance!.string2Message(messageString);
  return msg;
}