public void IMconfig() {
//BaseApplication里完成
//初始化
RongIM.init(this);
//新消息处理
RongIM.setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageListener() {
@Override
public boolean onReceived(final Message message, int i) {
//false 走融云默认方法 true走自己设置的方法
if (!isAppOnForeground() && message != null) {//app是否运行在后台 不在发消息推送广播
//未读消息数量
RongIMClient.getInstance().getTotalUnreadCount(new RongIMClient.ResultCallback<Integer>() {
@Override
public void onSuccess(Integer integer) {
messageNum=integer;
KLog.d("---IMMessageNum:"+integer);
//app后台运行 发送广播
Intent intent = new Intent();
intent.putExtra("SendId", message.getSenderUserId());//消息发送者
intent.putExtra("MsgType", message.getConversationType() + "");
intent.putExtra("MsgNum",messageNum+"");
intent.setAction("com.yu.chatdemo.receiver.ChatBoardcaseReceiver");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
BaseApplication.this.getApplicationContext().sendBroadcast(intent);
}
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
KLog.d("---IMMessageNumError:"+errorCode);
}
});
}
return true;
}
});
//userid对应的token
String toke = "";
//连接融云
RongIM.connect(toke, new RongIMClient.ConnectCallback() {
@Override
public void onTokenIncorrect() {
//Connect Token 失效的状态处理,需要重新获取 Token
KLog.d("--IMToken失效");
}
@Override
public void onSuccess(String userId) {
KLog.d("--IM连接成功");
}
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
KLog.d("--IM连接失败:"+errorCode);
}
});
//会话列表与会话页面的头像 昵称
RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
@Override
public UserInfo getUserInfo(String userid) {
//昵称与头像需要根据接口自己获得
return new UserInfo(userid,"名字", Uri.parse("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1088200534,4215449739&fm=116&gp=0.jpg"));
}
}, true);
}