集成环信的过程中,遇到各种坑,各种折磨。至于环信文档神马的我就不吐槽了。
EaseUI3.0作为Moduel导入,
image.png
一些不要的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<EMMessage> messages) {
//收到消息
}
@Override
public void onCmdMessageReceived(List<EMMessage> messages) {
//收到透传消息
}
@Override
public void onMessageRead(List<EMMessage> messages) {
//收到已读回执
}
@Override
public void onMessageDelivered(List<EMMessage> 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<EMMessage> messages) {
//收到消息
for (EMMessage message : messages) {
EaseUI.getInstance().getNotifier().onNewMsg(message);
}
refreshUIWithMessage();
}
@Override
public void onCmdMessageReceived(List<EMMessage> messages) {
//收到透传消息
LogHelper.trace("状态"+messages.get(0).getUserName()+",时间"+messages.get(0).getMsgTime());
}
@Override
public void onMessageRead(List<EMMessage> messages) {
//收到已读回执
}
@Override
public void onMessageDelivered(List<EMMessage> 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();
清单文件中,添加
<!-- 显示下载大图页面 -->
<activity
android:name="com.hyphenate.easeui.ui.EaseShowBigImageActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide" >
</activity>
<!-- 下载文件 -->
<activity
android:name="com.hyphenate.easeui.ui.EaseShowNormalFileActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide" >
</activity>
<!-- 地图 -->
<!-- 百度地图所需的service -->
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" />
<activity
android:name="com.hyphenate.easeui.ui.EaseBaiduMapActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide" >
</activity>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="EnlZGKw8rZnNhF31HvrPpFGzeVjnhDZg" />
点击图放大,以及百度地图 发送定位,其中百度地图 得自己去开放平台去注册 并获取他的key
2.离线消息(推送)
环信对android 平台给了三个推送,GCM推送,华为推送,小米推送,GCM由于墙的原因,直接pass,小米和华为,这里以小米为例
先去小米官网注册创建应用并获取到APPID APPKEY, APPSecret,并在环信后台上传证书
清单文件配置:
<!-- mi push -->
<service
android:name="com.xiaomi.mipush.sdk.PushMessageHandler"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedService" />
<service
android:name="com.xiaomi.mipush.sdk.MessageHandleService"
android:enabled="true" />
<receiver
android:name="com.hyphenate.chat.EMMipushReceiver"
android:exported="true"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" />
</intent-filter>
<intent-filter>
<action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" />
</intent-filter>
<intent-filter>
<action android:name="com.xiaomi.mipush.ERROR" />
</intent-filter>
</receiver>
<!-- mi push end -->
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<EMMessage> messages) {
for (EMMessage message : messages) {
if(!easeUI.hasForegroundActivies()){
//如果应用不在前台(或以杀死,因为小米推送的缘故杀不死),就把消息发送到通知栏上
easeUI.getNotifier().onNewMsg(message);
}
}
}
@Override
public void onCmdMessageReceived(List<EMMessage> messages) {
for (EMMessage message : messages) {
}
}
@Override
public void onMessageRead(List<EMMessage> messages) {
}
@Override
public void onMessageDelivered(List<EMMessage> 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;
}
});
网友评论