最近帮朋友弄im即时通信,简单记录下整个过程,以便以后用到:
一、Timsdk的集成
直接上腾讯云官网看文档就可以集成了;
https://www.qcloud.com/document/product/269/9227
二、初始化
1、登录TimSdk(登录前就要注册消息回调的接口):
public class Tecent_LogIn_Utils {
private String tag = "---->Tecent_LogIn_Utils";
private Context context;
private String identifier;
private String userSig;
private String groupId;
private int sdkAppId = 1400030792;
private TIMConversation conversation;
private int type;
private Message message;
private MyHandler myHandler;
private LogInImSdkCallBack resultCallBack;
public void logInImSdk(final Context context, String groupId, LogInImSdkCallBack resultCallBack, int type) {
this.type=type;
this.groupId=groupId;
this.context = context;
this.resultCallBack=resultCallBack;
this.userSig = SharedUtils.getSharedUtils().getData(context, "usersig");
this.identifier = SharedUtils.getSharedUtils().getData(context, "uid");
init();
logIn();
}
private void init() {
//初始化SDK基本配置
TIMSdkConfig config = new TIMSdkConfig(sdkAppId)
.enableCrashReport(false)
.enableLogPrint(true)
.setLogLevel(TIMLogLevel.DEBUG)
.setLogPath(Environment.getExternalStorageDirectory().getPath() + "/justfortest/");
//初始化SDK
TIMManager.getInstance().init(context, config);
TIMUserConfig userConfig = new TIMUserConfig()
.setUserStatusListener(new TIMUserStatusListener() {
@Override
public void onForceOffline() {
//被其他终端踢下线
Log.i(tag, "onForceOffline");
}
@Override
public void onUserSigExpired() {
//用户签名过期了,需要刷新userSig重新登录SDK
Log.i(tag, "onUserSigExpired");
}
})
//设置连接状态事件监听器
.setConnectionListener(new TIMConnListener() {
@Override
public void onConnected() {
Log.i(tag, "onConnected");
}
@Override
public void onDisconnected(int code, String desc) {
Log.i(tag, "onDisconnected");
}
@Override
public void onWifiNeedAuth(String name) {
Log.i(tag, "onWifiNeedAuth");
}
})
//设置群组事件监听器
.setGroupEventListener(new TIMGroupEventListener() {
@Override
public void onGroupTipsEvent(TIMGroupTipsElem elem) {
Log.i(tag, "onGroupTipsEvent, type: " + elem.getTipsType());
}
})
//设置会话刷新监听器
.setRefreshListener(new TIMRefreshListener() {
@Override
public void onRefresh() {
Log.i(tag, "onRefresh");
}
@Override
public void onRefreshConversation(List<TIMConversation> conversations) {
Log.e(tag, "onRefreshConversation, Send_RecMsg_Utils size: " + conversations.size());
}
});
//设置消息监听器,收到新消息时,通过此监听器回调
TIMManager.getInstance().addMessageListener(new TIMMessageListener() {
@Override
public boolean onNewMessages(List<TIMMessage> list) {
resultCallBack.onNewMessages(list,type);//新消息的回调,做了简单的实现;
return false;
}//消息监听器
});
TIMManager.getInstance().setUserConfig(userConfig);
}
private void logIn() {
message=new Message();
myHandler=new MyHandler(context,resultCallBack);
// identifier为用户名,userSig 为用户登录凭证
TIMManager.getInstance().login(identifier, userSig, new TIMCallBack() {
@Override
public void onError(int code, String desc) {
//错误码code和错误描述desc,可用于定位请求失败原因
Log.e(tag, "login failed. code: " + code + " errmsg: " + desc);
message.what=2;
message.obj = null;
message.arg1=Tecent_LogIn_Utils.this.type;
myHandler.sendMessage(message);
}
@Override
public void onSuccess() {
//获取会话
conversation=getGroupConversation(groupId);
message.what=1;
message.obj = conversation;
message.arg1=Tecent_LogIn_Utils.this.type;
myHand