Android 融雲IM整合以及使用詳解(一)
阿新 • • 發佈:2018-12-10
Android 融雲IM整合以及使用詳解(一)
整合
1.具體的整合步驟就不在詳細介紹,我們只說乾貨,附上融雲IM官方文件地址,裡面有更為詳細的整合介紹
https://www.rongcloud.cn/docs/#necessary
整合後效果
2.AndroidManifest配置融雲需要的配置
<!-- 融雲相關 --> <provider android:name="android.support.v4.content.FileProvider" android:authorities="自己的包名.FileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/rc_file_path"/> </provider> <!-- 單人會話介面 --> <activity android:name=".im.activity.ChatActivity" android:launchMode="singleTop" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden|adjustResize"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="替換成自己的包名" android:pathPrefix="/conversation/" android:scheme="rong" /> </intent-filter> </activity> <!-- 聚合會話列表 --> <activity android:name=".im.activity.SubConversationListActivity" android:launchMode="singleTask" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden|adjustResize"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="替換成自己的包名" android:path="/subconversationlist" android:scheme="rong" /> </intent-filter> </activity> <!-- 融雲訊息監聽 --> <!-- <receiver android:name=".receiver.SealNotificationReceiver"/> --> <receiver android:name=".receiver.SealNotificationReceiver" android:exported="true"> <intent-filter> <action android:name="io.rong.push.intent.MESSAGE_ARRIVED" /> <action android:name="io.rong.push.intent.MI_MESSAGE_ARRIVED" /> <action android:name="io.rong.push.intent.MESSAGE_CLICKED" /> <action android:name="io.rong.push.intent.MI_MESSAGE_CLICKED" /> <action android:name="io.rong.push.intent.THIRD_PARTY_PUSH_STATE" /> </intent-filter> </receiver>
3.融雲結構(各專案結構不同請自行參考)
4.融雲需要進行的配置
- 需要**在BaseApplication進行的配置
//初始化融雲 RongIM.init(this); //初始化聊天介面底部的自定義按鈕 具體在下面會詳細介紹 RongExtensionManager.getInstance().registerExtensionModule(RecognizeExtensionModule2.getInstence()); //設定融雲的訊息記錄的暱稱和頭像的內容提供者 RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() { @Override public UserInfo getUserInfo(String userId) { LogUtils.e("我執行了》》》》》》"+userId);//提供的是融雲的ID UserInfo userInfo = new UserInfo(userId, "", null); //在這裡通過融雲ID向自己的後臺請求使用者資料 return userInfo; } }, true); //群聊的使用者資料提供者 同上 RongIM.setGroupUserInfoProvider(new RongIM.GroupUserInfoProvider() { @Override public GroupUserInfo getGroupUserInfo(String s, String s1) { //設定資料 RongIM.getInstance().refreshGroupInfoCache(new Group(groupid, nickName, Uri.parse(avatar))); return getGroupUserInfos(s, s1); //向自己後臺獲取 } }, true); //融雲的配置 options = new DisplayImageOptions.Builder() .showImageForEmptyUri(R.drawable.de_default_portrait) .showImageOnFail(R.drawable.de_default_portrait) .showImageOnLoading(R.drawable.de_default_portrait) .displayer(new FadeInBitmapDisplayer(300)) .cacheInMemory(true) .cacheOnDisk(true) .build();
2.使用步驟
- 在自己的登入介面 呼叫自己的登入介面後呼叫融雲的連線介面
//Token是自己後臺返回 private void connect(String rongCloudToken, String nickName, String phono) { if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) { RongIM.connect(rongCloudToken, new RongIMClient.ConnectCallback() { /** * Token 錯誤。可以從下面兩點檢查 * 1. Token 是否過期,如果過期您需要向 App Server 重新請求一個新的 Token * 2. token 對應的 appKey 和工程裡設定的 appKey 是否一致 */ @Override public void onTokenIncorrect() { ToastView("--------------------Token相關問題"); } /** * 連線融雲成功 * @param userid 當前 token 對應的使用者 id */ @Override public void onSuccess(String userid) { LoadingView.dismissLoading(mShowLoading); Log.e("tag", "--------------------融雲登入成功>>>使用者id: " + userid); Intent intent = new Intent(LoginActivity.this, HomeActivity.class); intent.putExtra("noticeBean", gson.toJson(noticeBean)); startActivity(intent); //設定自己的使用者資訊 RongIM.getInstance().refreshUserInfoCache(new UserInfo(userid, nickName, Uri.parse(phono))); finish(); } /** * 連線融雲失敗 * @param errorCode 錯誤碼,可到官網 檢視錯誤碼對應的註釋 */ @Override public void onError(RongIMClient.ErrorCode errorCode) { ToastView("--------------------登入失敗" + errorCode); } }); } }
2.訊息介面
- 分為訊息記錄和好友介面 先附上兩張圖方便下面解釋
額…表示不全(拼音分類的還有列表哈)
- 效果看了下面講講具體的實現步驟(主介面就不用詳細介紹了 ViewPage + Fragment)
private void initFragment() {
mFragments = new ArrayList<>();
Fragment conversationFragment = new MessageFragment();
FriendFragment friendFragment = new FriendFragment();
mFragments.add(conversationFragment);
mFragments.add(friendFragment);
}
先看看訊息記錄的Fragment怎麼實現的
XML檔案:
<FrameLayout
android:id="@+id/mConversation"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
//重要程式碼
if(fragment != null){
transaction.remove(fragment);
}
//初始化融雲自己的介面
fragment = initConversationList();
//新增到佈局
transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mConversation, fragment);
transaction.commit();
/**
* 初始化會話列表
*
* @return
*/
private Fragment initConversationList() {
if (mConversationListFragment == null) {
ConversationListFragment listFragment = new ConversationListFragment();
listFragment.setAdapter(new ConversationListAdapterEx(RongContext.getInstance()));
Uri uri;
if (isDebug) {
uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
.appendPath("conversationlist")
.appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //設定私聊會話是否聚合顯示
.appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")//群組
.appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服務號
.appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//訂閱號
.appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系統
.appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "true")
.build();
mConversationsTypes = new Conversation.ConversationType[]{Conversation.ConversationType.PRIVATE,
Conversation.ConversationType.GROUP,
Conversation.ConversationType.PUBLIC_SERVICE,
Conversation.ConversationType.APP_PUBLIC_SERVICE,
Conversation.ConversationType.SYSTEM,
Conversation.ConversationType.DISCUSSION
};
} else {
uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
.appendPath("conversationlist")
.appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //設定私聊會話是否聚合顯示
.appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")//群組
.appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服務號
.appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//訂閱號
.appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系統
.build();
mConversationsTypes = new Conversation.ConversationType[]{Conversation.ConversationType.PRIVATE,
Conversation.ConversationType.GROUP,
Conversation.ConversationType.PUBLIC_SERVICE,
Conversation.ConversationType.APP_PUBLIC_SERVICE,
Conversation.ConversationType.SYSTEM
};
}
listFragment.setUri(uri);
mConversationListFragment = listFragment;
return listFragment;
} else {
return mConversationListFragment;
}
}
- 好友列表的Fragment參考Dome中就OK 裡面的程式碼可以直接Copy過來稍加修改就行,附上一張圖片
- 最後附上融雲提供的Dome地址,裡面很多東西 可以自行去研究
https://github.com/sealtalk/sealtalk-android
下篇將講解單聊聊天介面以及群裡聊天介面的使用以及坑…