首先看下官方的介绍的方法:
http://docs.easemob.com/im/490integrationcases/10nickname
官方只是介绍了理论实现方法,然而呢,众多小伙伴都是和我一样的啊,我们都是信封拿来主义的啊
既然如此,我就写这篇博客来记录下我的辛酸历程,供大家参考借鉴学习
我集成的只是easeui,并没有把官方的demo代码全部集成进去呐,这样的话我需要啥子功能,自己去demo中抠就好啦,好啦,大家都是这么干的啦
easeui集成步骤呢,这里就不细说了啊,官方说明的还是比较清楚的啦,下面是关于集成成功后的昵称和头像的设置办法
进入正题
一,首先看easeui中的代码,EaseChatFragment.java类中有个内部接口类EaseChatFragmentHelper,呀,看到这,忽然明白如果想自定义扩展字段,需要先自己实现这个方法啊,那就赶紧开工自己写个类继承EaseChatFragment并且实现这个接口类啊,上图看的真切些
@Override public void onSetMessageAttributes(EMMessage message) { if(isRobot){ //set message extension message.setAttribute("em_robot_message", isRobot); } //设置要发送扩展消息用户昵称 message.setAttribute(Constant.USER_NAME, "nike"); //设置要发送扩展消息用户头像 message.setAttribute(Constant.HEAD_IMAGE_URL, "http://img5.duitang.com/uploads/item/201508/30/20150830132007_TjANX.thumb.224_0.jpeg");
}
到此为止,就可以把昵称和头像发送出去啦
注意事项1:MyChatFragment实现了EaseChatFragmentHelper之后一定要调用EaseChatFragment中的setChatFragmentHelper(EaseChatFragmentHelper chatFragmentHelper)方法,调用地方截图如下:
注意事项2:在easyui中,最终把昵称和头像显示在控件上的代码是在EaseUserUtils这个类中的setUserAvatar()方法和setUserNick()方法,截图如下:
二,然后呢就是怎么接收昵称和头像啦,这个让我找的挺费劲的
这个还得从easeui初始化的时候说起,大家都知道,环信初始化必须得在application类中进行初始化,那么就从这个类着手吧,
边工作边写日志,想了想还是把代码贴出来吧,关键代码就是getUserInfo()方法中
public void init(Context context) { demoModel = new DemoModel(context); EMOptions options = initChatOptions(); //use default options if options is null if (EaseUI.getInstance().init(context, options)) { appContext = context; //获取easeui实例 easeUI = EaseUI.getInstance(); //初始化easeui easeUI.init(appContext,options); //在做打包混淆时,关闭debug模式,避免消耗不必要的资源 EMClient.getInstance().setDebugMode(true); setEaseUIProviders(); //设置全局监听 setGlobalListeners(); // broadcastManager = LocalBroadcastManager.getInstance(appContext); initDbDao(); } } private EMOptions initChatOptions(){ EMOptions options = new EMOptions(); // set if accept the invitation automatically options.setAcceptInvitationAlways(false); // set if you need read ack options.setRequireAck(true); // set if you need delivery ack options.setRequireDeliveryAck(false); return options; } protected void setEaseUIProviders() { // set profile provider if you want easeUI to handle avatar and nickname easeUI.setUserProfileProvider(new EaseUI.EaseUserProfileProvider() { @Override public EaseUser getUser(String username) { return getUserInfo(username); } }); } private EaseUser getUserInfo(String username){ //获取 EaseUser实例, 这里从内存中读取 //如果你是从服务器中读读取到的,最好在本地进行缓存 EaseUser user = null; //如果用户是本人,就设置自己的头像 if(username.equals(EMClient.getInstance().getCurrentUser())){ user=new EaseUser(username); user.setAvatar((String)SharedPreferencesUtils.getParam(appContext,APPConfig.USER_HEAD_IMG,"")); user.setNick((String)SharedPreferencesUtils.getParam(appContext, APPConfig.USER_NAME,"nike")); return user; } // if (user==null && getRobotList()!=null){ // user=getRobotList().get(username); // } //收到别人的消息,设置别人的头像 if (contactList!=null && contactList.containsKey(username)){ user=contactList.get(username); }else { //如果内存中没有,则将本地数据库中的取出到内存中 contactList=getContactList(); user=contactList.get(username); } //如果用户不是你的联系人,则进行初始化 if(user == null){ user = new EaseUser(username); EaseCommonUtils.setUserInitialLetter(user); }else { if (TextUtils.isEmpty(user.getAvatar())){//如果名字为空,则显示环信号码 user.setNick(user.getUsername()); } } Log.i("zcb","头像:"+user.getAvatar()); return user; } private void initDbDao() { inviteMessgeDao = new InviteMessgeDao(appContext); userDao = new UserDao(appContext); } public Map<String, RobotUser> getRobotList() { if (isLoggedIn() && robotList == null) { robotList = demoModel.getRobotList(); } return robotList; } /** * get current user's id */ public String getCurrentUsernName(){ if(username == null){ username = (String)SharedPreferencesUtils.getParam(appContext, Constant.HX_CURRENT_USER_ID,""); } return username; } /** *获取所有的联系人信息 * * @return */ public Map<String, EaseUser> getContactList() { if (isLoggedIn() && contactList == null) { contactList = demoModel.getContactList(); } // return a empty non-null object to avoid app crash if(contactList == null){ return new Hashtable<String, EaseUser>(); } return contactList; } /** * if ever logged in * * @return */ public boolean isLoggedIn() { return EMClient.getInstance().isLoggedInBefore(); } /** * set global listener */ protected void setGlobalListeners(){ registerMessageListener(); } /** * Global listener * If this event already handled by an activity, you don't need handle it again * activityList.size() <= 0 means all activities already in background or not in Activity Stack */ protected void registerMessageListener() { messageListener = new EMMessageListener() { private BroadcastReceiver broadCastReceiver = null; @Override public void onMessageReceived(List<EMMessage> messages) { for (EMMessage message : messages) { EMLog.d(TAG, "onMessageReceived id : " + message.getMsgId()); //接收并处理扩展消息 String userName=message.getStringAttribute(Constant.USER_NAME,""); String userId=message.getStringAttribute(Constant.USER_ID,""); String userPic=message.getStringAttribute(Constant.HEAD_IMAGE_URL,""); String hxIdFrom=message.getFrom(); System.out.println("helper接收到的用户名:"+userName+"helper接收到的id:"+userId+"helper头像:"+userPic); EaseUser easeUser=new EaseUser(hxIdFrom); easeUser.setAvatar(userPic); easeUser.setNick(userName); //存入内存 getContactList(); contactList.put(hxIdFrom,easeUser); //存入db UserDao dao=new UserDao(appContext); List<EaseUser> users=new ArrayList<EaseUser>(); users.add(easeUser); dao.saveContactList(users); // in background, do not refresh UI, notify it in notification bar // if(!easeUI.hasForegroundActivies()){ // getNotifier().onNewMsg(message); // } } } @Override public void onCmdMessageReceived(List<EMMessage> messages) { for (EMMessage message : messages) { EMLog.d(TAG, "receive command message"); //get message body //end of red packet code //获取扩展属性 此处省略 //maybe you need get extension of your message //message.getStringAttribute(""); } } @Override public void onMessageRead(List<EMMessage> messages) { } @Override public void onMessageDelivered(List<EMMessage> message) { } @Override public void onMessageChanged(EMMessage message, Object change) { } }; EMClient.getInstance().chatManager().addMessageListener(messageListener); }
至此可以正常显示自己和对方的头像和昵称了。
源码:https://github.com/zcbin88/HuanXinDemo
测试账号: 用户名:test 密码:123456
用户名 test1 密码:123456
欢迎吐槽...