您的位置:首页 > 移动开发 > Android开发

android 即时通讯 容云集成 融云会话界面 融云会话列表 头像和昵称设置

2017-12-25 14:31 726 查看
最近要做即时通信 就想到了环信和融云 综合考虑就选择了融云 有些坑 特此记录一下

简单说一下步骤

首先需要注册融云账号 创建融云项目 配置一下 获取appkey

第二要下载融云sdk

第三步 导入IMKit modle 和IMLib modle 并关联进app modle

第四步 配置融云的Appkey 配置到IMLib的清单文件 (IMLib —》AndroidManifast.xml )

meta-data

android:name=”RONG_CLOUD_APP_KEY”

android:value=”你的RONGIM的AppKEY” 需要的在配置一个内容支持者

provider

android:name=”android.support.v4.content.FileProvider”

android:authorities=”com.sxm.yiwei.FileProvider”

android:grantUriPermissions=”true”

android:exported=”false”>

meta-data

android:name=”android.support.FILE_PROVIDER_PATHS”

android:resource=”@xml/rc_file_path”/>

/provider

此时基本配置就完成了

第五步 就是初始化RongIM 了 在你的application的oncreate()方法中 初始化RongIM.init(this);

然后在再有就是动态获取token 这个一般是后台获取 融云文档也有的 需要的可以看一下 参数也就三个 用户id userid 用户名称 name 用户头像 headurl

第六步 获取到融云的token以后 我们链接一下融云服务器

public void connectRong(final Context context, final String rongToken){

if (context.getApplicationInfo().packageName.equals(getCurProcessName(context))) {

RongIM.connect(rongToken, new RongIMClient.ConnectCallback() {
// 重新获取token
/**
* Token 错误。可以从下面两点检查 1.  Token 是否过期,如果过期您需要向 App Server 重新请求一个新的 Token
*                  2.  token 对应的 appKey 和工程里设置的 appKey 是否一致
*/
@Override
public void onTokenIncorrect() {

Log.d("connectrong", "onTokenIncorrect: rong--token异常");
}

/**
* 连接融云成功
* @param userid 当前 token 对应的用户 id
*
*/
@Override
public void onSuccess(String userid) {
链接成功 根据需要求去启动会话列表或者会话界面
}

/**
* 连接融云失败
* @param errorCode 错误码,可到官网 查看错误码对应的注释
*/
@Override
public void onError(RongIMClient.ErrorCode errorCode) {

//排查原因 并重连

}
});
}

}

public static String getCurProcessName(Context context) {
int pid = android.os.Process.myPid();
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
if (appProcess.pid == pid) {
return appProcess.processName;
}
}
return null;
}


第七步 我们开始开启会话列表 首先创建一个activity 继承AppCompatActivity xml文件调用融云的fragment

fragment

android:id=”@+id/conversationlist”

android:name=”io.rong.imkit.fragment.ConversationListFragment”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

就这么多就好了

然后androidManifast.xml 文件配置一下

activity

android:name=”包名.Activity”

android:screenOrientation=”portrait”

android:windowSoftInputMode=”stateHidden|adjustResize”>

intent-filter>
<action android:name="android.intent.action.VIEW" />

category android:name="android.intent.category.DEFAULT" />

data
android:host="包名"
android:pathPrefix="/conversationlist"
android:scheme="rong" />
/intent-filter>
/activity>


我所需要的只是一个比较单一的功能 所以我每次进入到会话列表 我都会获取一下融云的token并链接一下融云服务器 确保链接正常 还有就是做会话列表 融云并不管理你的用户信息 所以你此处的聊天对象的头像和昵称有可能是不正确的 有可能是默认头像加userid 显示在这里 此处就需要我们自己设置 两个方案 后台返回 还有一个就是我们自己设置 因为后台不给 我只能使用了一个笨方法 融云虽然不管理我们的用户 但是我们可以通过融云获取到用户id列表 通过id 从自己的后台获取头像和昵称 设置给融云 并刷新列表 就可以正确显示会话列表的头像和昵称了

RongIM.getInstance().getConversationList(new RongIMClient.ResultCallbackList>() {

@Override

public void onSuccess(List conversations) {

for (int i = 0; i < conversations.size(); i++) {

getListUserInfo(RongChatListActivity.this,conversations.get(i).getTargetId());

setRongUserInfo(conversations.get(i).getTargetId());

}

}

@Override
public void onError(RongIMClient.ErrorCode errorCode) {

}
});


.getConversationList 这就是融云提供的会话列表信息 但是基本只有一个userid 可以用 我们使用RongIM.setUserInfoProvider 设置并返回给融云

//设置容云用户信息
private void setRongUserInfo(final String targetid) {
if (RongIM.getInstance()!=null){
RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
@Override
public UserInfo getUserInfo(String s) {

return null;
}
},true);
}

}
下载成功 设置并刷新就可以了


UserInfo userInfo = new UserInfo(otheruserid,othernickname,Uri.parse(otheravatar));

RongIM.getInstance().refreshUserInfoCache(userInfo);


启动会话列表 Map String,Boolean> map = new HashMap String, Boolean>();

map.put(Conversation.ConversationType.PRIVATE.getName(),false);//会话类型

RongIM.getInstance().startConversationList(context,map);

第八步 会话界面 同样需要我们建立一个activity

xml 文件 fragment

android:id=”@+id/conversation”

android:name=”io.rong.imkit.fragment.ConversationFragment”

android:layout_width=”match_parent”

android:layout_height=”match_parent” />

然后配置androidManifast.xml 文件

activity

android:name=”包名.Activity”

android:screenOrientation=”portrait”

android:windowSoftInputMode=”stateHidden|adjustResize”>

intent-filter>

action android:name=”android.intent.action.VIEW” />

category android:name="android.intent.category.DEFAULT" />

data
android:host="包名"
android:pathPrefix="/conversation/"
android:scheme="rong" />
/intent-filter>
/activity>、


融云服务器链接成功 直接启动就可以了

chatid 聊天对象id name 聊天对象名字

4000
RongIM.getInstance().startPrivateChat(context,chatId,name);

在会话界面 我使用的消息携带用户信息的方式 进行设置头像和昵称

第九步 最后一步 就是处理一下细节了 第一是消息提示 如果我们实在会话界面或会话列表可以直观的看到这个新消息提示的 如果不是 那就看不到 我可以给你一个提示 放退出会话列表或界面的 手动调用RongIM.disconnect()断开容云服务器 通过推送提示新消息 还有就是 当我们推出程序时 当收到消息点开融云会直接进入会话界面 这是你会发现没有消息 也不能回消息 这是因为你没有链接容云服务器 你需要在application中获取token 并连接容云服务器

还有就是你需要自定义一个容云的消息接收器

/**

* 作者: Nade_S on 2017/12/21.

* 自定义容云消息接收器

*/

public class MyRongReceiver extends PushMessageReceiver {

@Override

public boolean onNotificationMessageArrived(Context context, PushNotificationMessage pushNotificationMessage) {

return false;

}

@Override
public boolean onNotificationMessageClicked(Context context, PushNotificationMessage pushNotificationMessage) {

return false;
}


}

用于接受容云推送 不然你有可能就收不到

androidmanifast.xml 接收器配置

receiver

android:exported=”true”

android:name=”.包名.MyRongReceiver”>

intent-filter>

action android:name=”io.rong.push.intent.MESSAGE_ARRIVED” />

action android:name=”io.rong.push.intent.MI_MESSAGE_ARRIVED” />

action android:name=”io.rong.push.intent.MESSAGE_CLICKED” />

action android:name=”io.rong.push.intent.MI_MESSAGE_CLICKED” />

/intent-filter>

/receiver>

好 到此 融云集成就完成了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 即时通讯