接着上面寫
2.寫了單獨service來處理@相關數據
public class AtUserService {
private static AtUserService instance;
private static final String AT_GTOUP_IDS="at_group_ids";
public static AtUserService getInstance() {
if (instance == null) {
instance = new AtUserService();
}
return instance;
}
//發送時@人的列表
public List<TempUser> atUsers =new ArrayList<>();
public void addUser(User user){
TempUser tempUser=new TempUser();
tempUser.uid=user.id.toString();
tempUser.name=user.name;
atUsers.add(tempUser);
}
public List<String> getUserIds(String text){
if(atUsers==null||atUsers.size()==0){
return null;
}
ArrayList<String> ids=new ArrayList();
for(int i=0;i<atUsers.size();i++){
if(text.contains(atUsers.get(i).name)){
ids.add(atUsers.get(i).uid);
}
}
return ids;
}
//接受 :誰@了我的列表
private Set<String> atGroupIds =new HashSet<>();
public Set<String> getAtGroupIds(){
if(atGroupIds==null||atGroupIds.size()==0){
String string=UserConfigUtil.getStringConfig(AT_GTOUP_IDS,"");
if(string!=null&&!string.equals("")){
atGroupIds=GSONUtil.getGsonInstence().fromJson(string,new TypeToken<Set<String>>(){}.getType());
}
}
return atGroupIds;
}
public void addAtGroupId(String id){
atGroupIds.add(id);
UserConfigUtil.setConfig(AT_GTOUP_IDS,GSONUtil.getGsonInstence().toJson(atGroupIds),true);
}
public void removeAtGroupId(String id){
atGroupIds.remove(id);
UserConfigUtil.setConfig(AT_GTOUP_IDS,GSONUtil.getGsonInstence().toJson(atGroupIds),true);
}
public Set<String> curConversationId=new HashSet<>();
public void addCurConversationId(String id){
curConversationId.add(id);
}
public void removeCurConversationId(String id){
curConversationId.remove(id);
}
public class TempUser{
String uid;
String name;
}
}
3.RongService 裏處理和融雲相關的操作
- 發送消息
private void setSendMessageListener() {
if (RongIM.getInstance() != null) {
//設置自己發出的消息監聽器。
RongIM.getInstance().setSendMessageListener(new RongIM.OnSendMessageListener() {
/**
* 消息發送前監聽器處理接口(是否發送成功可以從SentStatus屬性獲取)。
*
* @param message 發送的消息實例。
* @return 處理後的消息實例。
*/
@Override
public Message onSend(Message message) {
MessageContent msgContent = message.getContent();
if(message.getConversationType().equals(Conversation.ConversationType.GROUP)&&msgContent.toString().contains("@")){
AtMsgBody msgBody=new AtMsgBody();
msgBody.groupId = message.getTargetId();
msgBody.senderName = AccountService.getInstance().me.name;
if(AtUserService.getInstance().getUserIds(((TextMessage) msgContent).getContent().toString())!=null){
List<String> ids=AtUserService.getInstance().getUserIds(((TextMessage) msgContent).getContent().toString());
String uids="";
for(String str:ids){
uids+=str+",";
extraJson.addProperty("uids",uids.equals("")?"":uids.substring(0,uids.length()-1));
uids=uids.equals("")?"":uids.substring(0,uids.length()-1);
AtUserService.getInstance().atUsers.clear();
}
}
((TextMessage) msgContent).setExtra(extraJson.toString());
}
return message;
}
- 接收到消息
RongIM.setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageListener() {
@Override
public boolean onReceived(Message message, int i) {
if(message.getConversationType().equals(Conversation.ConversationType.GROUP)&&message.getContent() instanceof TextMessage){
if(!AtUserService.getInstance().curConversationId.contains(message.getTargetId().toString())){
JsonObject jsonObject= GSONUtil.getGsonParser().parse(((TextMessage) message.getContent()).getExtra()).getAsJsonObject();
if(!jsonObject.isJsonNull()&& jsonObject.has("uids")&&!jsonObject.get("uids").isJsonNull()){
String strUids=jsonObject.get("uids").getAsString();
if(strUids.contains(AccountService.getInstance().me.id.toString())){
AtUserService.getInstance().addAtGroupId(message.getTargetId().toString());
}
}
}
}
}
- 消息點擊後取消@顯示
RongIM.setConversationListBehaviorListener(new RongIM.ConversationListBehaviorListener() {
@Override
public boolean onConversationClick(Context context, View view, final UIConversation uiConversation) {
if(uiConversation.getConversationType().equals(Conversation.ConversationType.GROUP)
&&AtUserService.getInstance().getAtGroupIds().contains(uiConversation.getConversationTargetId())){
AtUserService.getInstance().removeAtGroupId(uiConversation.getConversationTargetId());
}
}
}