集成环信的过程中,遇到各种坑,各种折磨。至于环信文档神马的我就不吐槽了。
EaseUI3.0作为Moduel导入,
一些不要的so文件该去掉,还是去掉吧,apk 会很大的有木有,
1.在线消息
1.1 简单的会话列表
在你新建的Activiy或者Fragment 如下代码
conversationListFragment = new EaseConversationListFragment();
conversationListFragment.setConversationListItemClickListener(new EaseConversationListFragment.EaseConversationListItemClickListener() {
@Override
public void onListItemClicked(EMConversation conversation) {
startActivity(new Intent(CustomeServiceActivity.this, EMChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, conversation.conversationId()));
}
});
getSupportFragmentManager().beginTransaction().add(R.id.container, conversationListFragment).commit();
第一个坑(也怪自己理解能力弱,弱鸡~),在其他人发过来消息,这个列表上出现,红色小圆点badge。这个时候,我直接去环信官网扒文档,扒出如下代码
EMClient.getInstance().chatManager().addMessageListener(msgListener);
EMMessageListener msgListener = new EMMessageListener() {
@Override
public void onMessageReceived(List messages) {
//收到消息
}
@Override
public void onCmdMessageReceived(List messages) {
//收到透传消息
}
@Override
public void onMessageRead(List messages) {
//收到已读回执
}
@Override
public void onMessageDelivered(List message) {
//收到已送达回执
}
@Override
public void onMessageChanged(EMMessage message, Object change) {
//消息状态变动
}
};
记得在不需要的时候移除listener,如在activity的onDestroy()时
EMClient.getInstance().chatManager().removeMessageListener(msgListener);
然而如此设置 没卵用啊,又去论坛问,没有回应,最后还是摸索出来了...
正确如下:
onResume
@Override
protected void onResume() {
super.onResume();
EaseUI.getInstance().pushActivity(this);
EMClient.getInstance().chatManager().addMessageListener(msgListener);
}
onStop
@Override
protected void onStop() {
EMClient.getInstance().chatManager().removeMessageListener(msgListener);
EaseUI.getInstance().popActivity(this);
super.onStop();
}
设置监听
EMMessageListener msgListener = new EMMessageListener() {
@Override
public void onMessageReceived(List messages) {
//收到消息
for (EMMessage message : messages) {
EaseUI.getInstance().getNotifier().onNewMsg(message);
}
refreshUIWithMessage();
}
@Override
public void onCmdMessageReceived(List messages) {
//收到透传消息
LogHelper.trace("状态"+messages.get(0).getUserName()+",时间"+messages.get(0).getMsgTime());
}
@Override
public void onMessageRead(List messages) {
//收到已读回执
}
@Override
public void onMessageDelivered(List message) {
//收到已送达回执
}
@Override
public void onMessageChanged(EMMessage message, Object change) {
//消息状态变动
LogHelper.trace("状态"+String.valueOf(change));
}
};
private void refreshUIWithMessage() {
runOnUiThread(new Runnable() {
public void run() {
// refresh conversation list
if (conversationListFragment != null) {
conversationListFragment.refresh();
}
}
});
}
至于其他地方也需要设置未读消息小红点,例如首页等,可以同样的设置监听 并调用(未读消息)
EMClient.getInstance().chatManager().getUnreadMsgsCount();
有个问题 设置小红点的时候,必须在主线程中,不然没效果。。。
1.2 聊天界面
聊天界面就简单了
EaseChatFragment chatFragment = new EaseChatFragment();
//传入参数
chatFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
清单文件中,添加
点击图放大,以及百度地图 发送定位,其中百度地图 得自己去开放平台去注册 并获取他的key
2.离线消息(推送)
环信对android 平台给了三个推送,GCM推送,华为推送,小米推送,GCM由于墙的原因,直接pass,小米和华为,这里以小米为例
先去小米官网注册创建应用并获取到APPID APPKEY, APPSecret,并在环信后台上传证书
清单文件配置:
java代码部分
Application 的 onCreate中
@Override
public void onCreate() {
easeUI = EaseUI.getInstance();
EMOptions options = new EMOptions();
options.setAutoLogin(false);
options.setMipushConfig("2882303761517595100","5261759563100");
easeUI.init(this, options);
registerMessageListener();
}
//全局监听,后台应用或者被杀死,从这里接收
protected void registerMessageListener() {
EMMessageListener messageListener = new EMMessageListener() {
private BroadcastReceiver broadCastReceiver = null;
@Override
public void onMessageReceived(List messages) {
for (EMMessage message : messages) {
if(!easeUI.hasForegroundActivies()){
//如果应用不在前台(或以杀死,因为小米推送的缘故杀不死),就把消息发送到通知栏上
easeUI.getNotifier().onNewMsg(message);
}
}
}
@Override
public void onCmdMessageReceived(List messages) {
for (EMMessage message : messages) {
}
}
@Override
public void onMessageRead(List messages) {
}
@Override
public void onMessageDelivered(List message) {
}
@Override
public void onMessageChanged(EMMessage message, Object change) {
}
};
EMClient.getInstance().chatManager().addMessageListener(messageListener);
}
如果想处理点击通知栏消息的逻辑,如下
easeUI.getNotifier().setNotificationInfoProvider(new EaseNotifier.EaseNotificationInfoProvider() {
@Override
public String getDisplayedText(EMMessage message) {
return null;
}
@Override
public String getLatestText(EMMessage message, int fromUsersNum, int messageNum) {
return null;
}
@Override
public String getTitle(EMMessage message) {
return null;
}
@Override
public int getSmallIcon(EMMessage message) {
return 0;
}
@Override
public Intent getLaunchIntent(EMMessage message) {
return null;
}
});