sendMessage method

  1. @override
Future<Map<String, dynamic>> sendMessage(
  1. CreateMsgType createMsgType,
  2. CreateMsgInfo createMsgInfo, {
  3. Map<String, dynamic>? options,
})
override

Implementation

@override
Future<Map<String, dynamic>> sendMessage(CreateMsgType createMsgType, CreateMsgInfo createMsgInfo, {Map<String, dynamic>? options}) async {
  if (ImState.chatInstance.value != null) {
    V2TimValueCallback<V2TimMsgCreateInfoResult>? createMessageRes;

    if (createMsgType == CreateMsgType.text) {
      createMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createTextMessage(
        text: createMsgInfo.payload,
      );
    } else if (createMsgType == CreateMsgType.image) {
      createMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createImageMessage(
        imagePath: "test",
      );
    } else if (createMsgType == CreateMsgType.custom) {
      createMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().createCustomMessage(
        data: createMsgInfo.payload,
        desc: '',
        extension: '',
      );
    }

    if (createMessageRes == null) {
      developerLog('消息发送失败,无法创建要发送的消息对象,因为 createMsgType 未知或暂不支持');
      throw Exception('消息发送失败,无法创建要发送的消息对象,因为 createMsgType 未知或暂不支持');
    }

    if (createMessageRes.code == 0) {
      V2TimValueCallback<V2TimMessage> sendMessageRes = await TencentImSDKPlugin.v2TIMManager.getMessageManager().sendMessage(
          message: createMessageRes.data?.messageInfo,
          receiver: createMsgInfo.conversationType == ConversationType.convC2c ? createMsgInfo.to : '',
          groupID: createMsgInfo.conversationType == ConversationType.convGroup ? createMsgInfo.to : '',
          priority: MessagePriorityEnum.V2TIM_PRIORITY_DEFAULT, // 消息优先级
          onlineUserOnly: false, // 是否只有在线用户才能收到,如果设置为 true ,接收方历史消息拉取不到,常被用于实现“对方正在输入”或群组里的非重要提示等弱提示功能,该字段不支持 AVChatRoom。
          isExcludedFromUnreadCount: false, // 发送消息是否从会话未读数中排除
          isExcludedFromLastMessage: false, // 发送消息是否从会话 lastMessage 中排除
          needReadReceipt: false, // 消息是否需要已读回执(只有 Group 消息有效,6.1 及以上版本支持,需要您购买旗舰版或企业版套餐)
          offlinePushInfo: null, // 离线推送时携带的标题和内容
          cloudCustomData: "", // 消息云端数据,消息附带的额外的数据,存云端,消息的接收者可以访问到
          localCustomData: "" // 消息本地数据,消息附带的额外的数据,存本地,消息的接收者不可以访问到,App 卸载后数据丢失
      );
      if (sendMessageRes.code == 0) {
        // 发送成功
        return sendMessageRes.toJson();
      } else {
        developerLog('消息发送失败, ${sendMessageRes.code} | ${sendMessageRes.desc}');
        throw Exception('消息发送失败, ${sendMessageRes.code} | ${sendMessageRes.desc}');
      }
    } else {
      developerLog('创建消息失败, ${createMessageRes.code} | ${createMessageRes.desc}');
      throw Exception('创建消息失败, ${createMessageRes.code} | ${createMessageRes.desc}');
    }
  } else {
    developerLog('sdk 未初始化');
    throw Exception('sdk 未初始化');
  }
}