在某些情况下开发者希望UI全部自己来完成,IM消息数据通道使用SDK,这种情况下,可以使用IMCore的方式来集成,OpenIMCore只提供数据通道,不包含任何UI组件。当然,并不是说使用了SDK的UI,就不能使用IMCore,开发者可以在使用IMKit的同时使用IMCore。
请参考快速集成方式的初始化
//获取imcore对象,可保存为全局变量,供APP使用 //未使用IMKit的情况下,创建IMCore对象 final String userID = "testpro1"; final String appkey = "23015524" YWIMCore imCore = YWAPI.createIMCore(userID, appkey);
如果是使用IMKit的情况下,IMCore对象的获取,需要从IMKit对象中获取,不能再另外创建。
IMKit对象的获取,请参考
//从IMKit对象中获取IMCore IMCore imCore = mIMKit.getIMCore()
//添加连接状态监听,即登录状态监听 imCore.addConnectionListener(new IYWConnectionListener() { @Override public void onDisconnect(int i, String s) { //掉线 } @Override public void onReConnecting() { //正在重登 } @Override public void onReConnected() { //重登成功 } }); //在登录前注册消息push的监听通知,以便登录成功后即能收取到消息的通知 //请尽早添加该监听,从而保证应用不会因为在登录成功后再添加通知而错过一些消息通知 imCore.getConversationService().addPushListener(new IYWPushListener() { @Override //收到单聊消息时会回调该方法,开发者可以在该方法内更新该会话的未读数 public void onPushMessage(IYWContact arg0, YWMessage arg1) { //单聊消息 YWConversation conversation = imCore.getConversationService().getConversationByUserId(arg0.getUserId()); //如果是客服会话,需要这样获取: //EServiceContact contact1 = new EServiceContact(AccountUtils.getMainAccouintId(contact.getUserId())); //YWConversation conversation = imCore.getConversationService().getConversation(contact1); int unreadCount = conversation.getUnreadCount(); //TODO 更新UI上该会话未读数 } @Override //收到群聊消息时会回调该方法,开发者可以在该方法内更新该会话的未读数 public void onPushMessage(YWTribe arg0, YWMessage arg1) { //群消息 YWConversation conversation = imCore.getConversationService().getTribeConversation(arg0.getTribeId()); int unreadCount = conversation.getUnreadCount(); //TODO 更新UI上该会话未读数 } }); //注册消息未读总数监听 imCore.getConversationService().addTotalUnreadChangeListener(new IYWConversationUnreadChangeListener() { @Override //当前登录账号的未读消息总数发送变化时会回调该方法,用户可以再该方法中更新UI的未读数 public void onUnreadChange() { int totalUnreadCount = imCore.getConversationService().getAllUnreadCount(); //TODO 更新UI的未读数 } });
使用与OpenIM服务器商定的userId,password即可登录到旺信OpenIM服务器。
//开始登录 String userid = "testpro1"; String password = "taobao1234"; IYWLoginService loginService = imCore.getLoginService(); YWLoginParam Param = YWLoginParam.createLoginParam(userId, password); loginService.login(Param, new IWxCallback() { @Override public void onSuccess(Object... arg0) { //登录成功 } @Override public void onProgress(int arg0) { // TODO Auto-generated method stub } @Override public void onError(int errCode, String description) { //如果登录失败,errCode为错误码,description是错误的具体描述信息 } });
OpenIM内部会自动读取OpenAccount的登录状态,开发者在成功登录OpenAccount后,只要调用OpenIM的登录即可,
不需要传任何用户相关的信息,SDK内部会自动处理。
IYWLoginService mLoginService = mIMKit.getLoginService(); mLoginService.login(null, callback); //登录参数传null
IYWLoginService loginService = imCore.getLoginService(); loginService.logout(new IWxCallback() { @Override public void onSuccess(Object... arg0) { //登出成功 } @Override public void onProgress(int arg0) { // TODO Auto-generated method stub } @Override public void onError(int errCode, String description) { //登出失败,errCode为错误码,description是错误的具体描述信息 } });
会话列表的获取,消息的发送等等,请查看