sendMessage static method

Future<V2TimValueCallback<V2TimMessage>> sendMessage({
  1. required String id,
  2. required String receiver,
  3. required String groupID,
  4. int priority = 0,
  5. bool onlineUserOnly = false,
  6. bool isExcludedFromUnreadCount = false,
  7. bool isExcludedFromLastMessage = false,
  8. bool? isSupportMessageExtension = false,
  9. bool needReadReceipt = false,
  10. bool? isExcludedFromContentModeration = true,
  11. Map<String, dynamic>? offlinePushInfo,
  12. String? cloudCustomData,
  13. String? localCustomData,
})

Implementation

static Future<V2TimValueCallback<V2TimMessage>> sendMessage({
  required String id,
  required String receiver,
  required String groupID,
  int priority = 0,
  bool onlineUserOnly = false,
  bool isExcludedFromUnreadCount = false,
  bool isExcludedFromLastMessage = false,
  bool? isSupportMessageExtension = false,
  bool needReadReceipt = false,
  bool? isExcludedFromContentModeration = true,
  Map<String, dynamic>? offlinePushInfo,
  // 自定义消息需要
  String? cloudCustomData, // 云自定义消息字段,只能在消息发送前添加
  String? localCustomData,
}) async {
  if (!createdMessage.containsKey(id)) {
    return V2TimValueCallback<V2TimMessage>.fromJson({
      "code": -1,
      "desc": "id not exist please try create again",
    });
  } else {
    Map<String, dynamic> message = createdMessage.remove(id)!;
    Map<String, dynamic> fullmessage = Tools.createNativeSendMessage(
      createdMessage: message,
      groupId: groupID,
      userID: receiver,
      priority: priority,
      onlineUserOnly: onlineUserOnly,
      isExcludedFromLastMessage: isExcludedFromLastMessage,
      isExcludedFromUnreadCount: isExcludedFromUnreadCount,
      needReadReceipt: needReadReceipt,
      localCustomData: localCustomData,
      cloudCustomData: cloudCustomData,
      isSupportMessageExtension: isSupportMessageExtension,
      isExcludedFromContentModeration: isExcludedFromContentModeration,
      offlinePushInfo: offlinePushInfo != null ? Tools.generateNativeOfflinePushInfoByDart(OfflinePushInfo.fromJson(offlinePushInfo)) : null,
    );
    String userData = Tools.generateUserData();
    Pointer<Uint8> user_data = Tools.string2PointerInt8(userData);
    Pointer<Uint8> json_msg_param = Tools.string2PointerInt8(json.encode(fullmessage));
    Pointer<Uint8> conv_id = Tools.string2PointerInt8(groupID.isEmpty ? receiver : groupID);
    int res = desktopSDK.D_TIMMsgSendMessage(
      conv_id,
      groupID.isEmpty ? 1 : 2,
      json_msg_param,
      user_data,
    );
    if (res != TIMResult.TIM_SUCC) {
      return V2TimValueCallback<V2TimMessage>.fromJson({
        "code": res,
        "desc": "",
      });
    } else {
      Map<String, dynamic> data = await getAsyncData(
        apiKey: userData,
        maxtimeout: 20000,
      );
      Map<String, dynamic> nativeData = Map<String, dynamic>.from(json.decode(data["json_param"].isEmpty ? json.encode({}) : data["json_param"]));

      Map<String, dynamic> DartData = (await Tools.convertMessage2Dart(nativeData)).toJson();
      DartData["id"] = id; // add id after send
      return V2TimValueCallback<V2TimMessage>.fromJson({
        "code": data["code"],
        "desc": data["desc"],
        "data": DartData,
      });
    }
  }
}