环信设置头像和昵称

 随手记录一下环信设置头像和昵称的方法,方便以后查看

1.登录成功后调用:将头像和昵称保存并调用

// 将自己服务器返回的环信账号、昵称和头像URL设置到帮助类中。
DemoHelper.getInstance().getUserProfileManager().setCurrentUserNick(userInfo.getObj().getName());
DemoHelper.getInstance().getUserProfileManager().setCurrentUserAvatar(userInfo.getObj().getHeadimg());
DemoHelper.getInstance().setCurrentUserName(tv_phone.getText().toString()); 


2.使用拓展消息:

定位到ChatFragment 的 onSetMessageAttributes
@Override
public void onSetMessageAttributes(EMMessage message) {
    if(isRobot){
        //set message extension
        message.setAttribute("em_robot_message", isRobot);
    }
    // 通过扩展属性,将Pic和userName发送出去。
    String Pic = Preference.instance(getActivity()).getUserImg();
    if (!TextUtils.isEmpty(Pic)) {
        message.setAttribute("Pic", Pic);
    }
    Log.d("tag","图片地址"+Pic);
    String userName = Preference.instance(getActivity()).getUsername();
    if (!TextUtils.isEmpty(userName)) {
        message.setAttribute("userName", userName);
    }
    Log.d("tag","昵称"+userName);
}

3.接收头像和昵称:
定位到demoHelper的 onMessageReceived方法
@Override
public void onMessageReceived(List messages) {
    for (EMMessage message : messages) {
                 //************接收并处理扩展消息***********************
                 String userName = message.getStringAttribute("userName", "");
                 String Pic = message.getStringAttribute("Pic", "");
                 String hxIdFrom = message.getFrom();
                 EaseUser easeUser = new EaseUser(hxIdFrom);
                 easeUser.setAvatar(Pic);
                 easeUser.setNick(userName);
                 // 存入内存
                 getContactList();
                 contactList.put(hxIdFrom, easeUser);
                 // 存入db
                 UserDao dao = new UserDao(appContext);
                 List users = new ArrayList();
                 users.add(easeUser);
                 dao.saveContactList(users);
                 getModel().setContactSynced(true);
                 // 通知listeners联系人同步完毕
                 notifyContactsSyncListener(true);
                 if (isGroupsSyncedWithServer()) {
                    noitifyGroupSyncListeners(true);
                 }
                 // ******************扩展信息处理完成**********************
        // in background, do not refresh UI, notify it in notification bar
        if(!easeUI.hasForegroundActivies()){
            getNotifier().onNewMsg(message);
        }
    }
}
 
  
 
  
 
  
 
  

4.显示:
 
  
 
  
定位到demoHelper的 getUserInfo方法
private EaseUser getUserInfo(String username){
      // To get instance of EaseUser, here we get it from the user list in memory
      // You'd better cache it if you get it from your server
        EaseUser user = null;
        if(username.equals(EMClient.getInstance().getCurrentUser()))
            return getUserProfileManager().getCurrentUserInfo();
        user = getContactList().get(username);
        if(user == null && getRobotList() != null){
            user = getRobotList().get(username);
        }
        if (contactList != null && contactList.containsKey(username)) {
        } else { // 如果内存中没有,则将本地数据库中的取出到内存中。
            getContactList();
        }
        user = contactList.get(username);
        // if user is not in your contacts, set inital letter for him/her
        if(user == null){
            user = new EaseUser(username);
            EaseCommonUtils.setUserInitialLetter(user);
        }else{
            if(TextUtils.isEmpty(user.getNick())){ // 如果名字为空,则显示环信号码
                user.setNick(user.getUsername());
            }
        }
        return user;
   }

以上四部曲显示头像和昵称完成。
 
  
 
  
 
  
 
  
 
  
 
  




你可能感兴趣的:(环信设置头像和昵称)