1.腾讯IM文档地址
https://cloud.tencent.com/document/product/269/1557#.E5.88.9B.E5.BB.BA.E5.BA.94.E7.94.A8
2.下面有两个工具类
/** * 初始化 * 包括imsdk等 * 文档: */ public class InitBusiness { private static final String TAG = InitBusiness.class.getSimpleName(); private InitBusiness() { } public static void start(Context context) { initImsdk(context, 0); } public static void start(Context context, int logLevel) { initImsdk(context, logLevel); } /** * 初始化imsdk */ private static void initImsdk(Context context, int logLevel) { Log.e("TIM", "initIMsdk"); TIMSdkConfig config = new TIMSdkConfig(Globals.SDK_APPID) .enableCrashReport(Globals.IS_0PEN_BUGLY_CRASH); //初始化imsdk TIMManager.getInstance().init(context, config); Log.e("TIM", "initIMsdk1111"); } /** * 用户状态变更 */ public static void setUserListener() { TIMUserConfig userConfig = new TIMUserConfig(); userConfig.setUserStatusListener(new TIMUserStatusListener() { @Override public void onForceOffline() { //被踢下线时回调 SharedPreferencesUtil.removeLoginState(MainArtActivity.mContext); final MyDialog dialog = new MyDialog(MainArtActivity.mContext, MainArtActivity.mContext.getString(R.string.log_back_in_2), ""); dialog.setOne(); dialog.show(); dialog.setOk(Globals.context.getString(R.string.log_back_in), new View.OnClickListener() { @Override public void onClick(View v) { Globals.context.startActivity(new Intent(MainArtActivity.mContext, LoginActivty.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); dialog.dismiss(); } }); } @Override public void onUserSigExpired() { //票据过期时回调 SharedPreferencesUtil.removeLoginState(MainArtActivity.mContext); final MyDialog dialog = new MyDialog(MainArtActivity.mContext, MainArtActivity.mContext.getString(R.string.online_time_long), ""); dialog.setOne(); dialog.show(); dialog.setOk(Globals.context.getString(R.string.log_back_in), new View.OnClickListener() { @Override public void onClick(View v) { Globals.context.startActivity(new Intent(MainArtActivity.mContext, LoginActivty.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); dialog.dismiss(); } }); } }); TIMManager.getInstance().setUserConfig(userConfig); } /** * 新消息通知 */ public static void addMessageListener() { //设置消息监听器,收到新消息时,通过此监听器回调 TIMManager.getInstance().addMessageListener(new TIMMessageListener() {//消息监听器 @Override public boolean onNewMessages(List<TIMMessage> list) {//收到新消息 for (int i = 0; i < list.size(); i++) { TIMMessage msg = list.get(i); if (msg.getConversation().getType().toString().equals("C2C")) { //获取当前元素的类型 TIMElem elem = msg.getElement(0); TIMElemType elemType = elem.getType(); if (elemType == TIMElemType.Custom) { TIMCustomElem custom = (TIMCustomElem) elem; try { String s1 = new String(custom.getData(), "utf-8"); //TODO 新消息通知 Log.e("TIM", "addMessageListener---" + s1); // EventBus.getDefault().post(new MessageEvent(s1)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } return false; } }); } }
/** * 登录 */ public class LoginBusiness { private static final String TAG = "LoginBusiness"; private LoginBusiness() { } /** * 登录imsdk * * @param identify 用户id * @param userSig 用户签名 * @param callBack 登录后回调 */ public static void loginIm(String identify, String userSig, TIMCallBack callBack) { if (identify == null || userSig == null) return; //发起登录请求 TIMManager.getInstance().login( identify, userSig, //用户帐号签名,由私钥加密获得,具体请参考文档 callBack); } /** * 登出imsdk * * @param callBack 登出后回调 */ public static void logout(TIMCallBack callBack){ TIMManager.getInstance().logout(callBack); } }
3.在Application中进行初始化
//Tim 注册离线推送监听器 if (MsfSdkUtils.isMainProcess(this)) { TIMManager.getInstance().setOfflinePushListener(new TIMOfflinePushListener() { @Override public void handleNotification(TIMOfflinePushNotification notification) { if (notification.getGroupReceiveMsgOpt() == TIMGroupReceiveMessageOpt.ReceiveAndNotify) { //消息被设置为需要提醒 notification.doNotify(getApplicationContext(), R.mipmap.ic_launcher); } } }); } //初始化Tim InitBusiness.start(getApplicationContext()); InitBusiness.setUserListener(); InitBusiness.addMessageListener();
4.接受消息
实现 TIMMessageListener接口