1. 程式人生 > >整合EaseUI3.0 線上訊息和離線訊息

整合EaseUI3.0 線上訊息和離線訊息

整合環信的過程中,遇到各種坑,各種折磨。至於環信文件神馬的我就不吐槽了。

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<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;
            }
        });