Files
yusheng-android/BaseModule/src/main/java/com/xscm/moduleutil/bean/RoomMessageManager.java
2025-11-07 09:22:39 +08:00

85 lines
2.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xscm.moduleutil.bean;
import com.blankj.utilcode.util.GsonUtils;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMSendCallback;
import com.xscm.moduleutil.bean.room.Children;
import com.xscm.moduleutil.bean.room.EmotionDeatils;
import com.xscm.moduleutil.event.RoomTaskEvent;
import com.xscm.moduleutil.utils.SpUtil;
import org.greenrobot.eventbus.EventBus;
import java.nio.charset.StandardCharsets;
import java.util.Random;
public class RoomMessageManager {
public String roomId;
private static RoomMessageManager instance ;
public String getRoomId() {
return roomId;
}
public void setRoomId(String roomId) {
this.roomId = roomId;
}
public static RoomMessageManager getInstance() {
if (instance == null){
instance = new RoomMessageManager();
}
return instance;
}
public void sendChatEmoji(EmotionDeatils emotionDeatils){
EmotionDeatils event = new EmotionDeatils();
if (emotionDeatils != null&& emotionDeatils.getChildren()!=null && !emotionDeatils.getChildren().isEmpty() && emotionDeatils.getChildren().size()>0){
int position = new Random().nextInt(emotionDeatils.getChildren().size());
Children children = emotionDeatils.getChildren().get(position);
event.setImage(children.getImage());
event.setAnimate_image(emotionDeatils.getAnimate_image());
}else {
event= emotionDeatils;
}
RoomMessageEvent roomMessageEvent=new RoomMessageEvent();
roomMessageEvent.setRoomId(roomId);
roomMessageEvent.setMsgType(2);//这是表情类型1单发文本消息 2单发表情消息
UserInfo userInfo= SpUtil.getUserInfo();
RoomMessageEvent.T t=new RoomMessageEvent.T();
t.setFromUserInfo(userInfo);
t.setEmoji(event);
roomMessageEvent.setText(t);
String json = GsonUtils.toJson(roomMessageEvent);
// 转换为 byte[]
byte[] binaryData = json.getBytes(StandardCharsets.UTF_8);
// 创建自定义消息
V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createCustomMessage(binaryData);
// 发送消息
V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, null, "room" + roomId, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {
@Override
public void onProgress(int progress) {
// 自定义消息不会回调进度
}
@Override
public void onSuccess(V2TIMMessage message) {
// 发送群聊自定义消息成功
// EventBus.getDefault().post(new RoomTaskEvent());
}
@Override
public void onError(int code, String desc) {
// 发送群聊自定义消息失败
}
});
}
}