Android 融雲IM整合以及使用詳解(二)
阿新 • • 發佈:2018-12-10
Android 融雲IM整合以及使用詳解(二)
上篇講解了整合和好友列表和訊息記錄的使用,這篇將講解聊天介面和群聊介面的使用
-
先附上一張效果圖
-
先介紹佈局檔案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" tools:context=".im.activity.ChatActivity"> //這裡要注意 //自行在這裡新增自己的頭部 <View android:layout_width="match_parent" android:layout_height="0.3dp" android:background="#ededed" /> <fragment android:id="@+id/conversation" android:name="io.rong.imkit.fragment.ConversationFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" /> </LinearLayout>
-
程式碼中的使用
targetId = getIntent().getData().getQueryParameter("targetId"); //傳遞的融雲的id String title = getIntent().getData().getQueryParameter("title"); //傳遞的融雲名稱/使用者暱稱 //需要先設定使用者資料提供者 上篇部落格有講解
-
監聽器的設定
//融雲傳送訊息的監聽 RongIM.getInstance().setSendMessageListener(new RongIM.OnSendMessageListener() { @Override public Message onSend(Message message) { Log.e("tag", "------------------------------message----message-----------------------------"); @Override public boolean onSent(Message message, RongIM.SentMessageErrorCode sentMessageErrorCode) { return false; } }); //融雲訊息的點選監聽 RongIM.setConversationClickListener(this); @Override public boolean onUserPortraitClick(Context context, Conversation.ConversationType conversationType, UserInfo userInfo, String targetId) { //點選使用者頭像的監聽 String rongUserId = userInfo.getUserId(); return false; } @Override public boolean onUserPortraitLongClick(Context context, Conversation.ConversationType conversationType, UserInfo userInfo, String s) { return false; } @Override public boolean onMessageClick(Context context, View view, Message message) { //在這裡可以判斷點選訊息的型別 進行不同的跳轉 if (message.getContent() instanceof LocationMessage) { Intent intent = new Intent(ChatActivity.this, GuideActivity.class); intent.putExtra("location", message.getContent()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); return true; } else { return false; } }
-
群聊訊息的介面
還是先附上一張效果圖
注意需要在配置清單裡面配置介面,在聊天記錄介面初始化列表時開啟聚合會話
下面是XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.leyongleshi.ljd.view.HeadTitleLinearView android:id="@+id/mHeadView" android:layout_width="match_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@+id/rong_content" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
再附上全部程式碼
/**
* Created by Bob on 15/11/3.
* 聚合會話列表
*/
public class SubConversationListActivity extends BaseActivity {
@BindView(R.id.mHeadView)
HeadTitleLinearView mMHeadView;
@BindView(R.id.rong_content)
FrameLayout mRongContent;
@Override
public int getResId() {
return R.layout.activity_rong;
}
@Override
public void initView() {
SubConversationListFragment fragment = new SubConversationListFragment();
fragment.setAdapter(new SubConversationListAdapterEx(RongContext.getInstance()));
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.rong_content, fragment);
transaction.commit();
Intent intent = getIntent();
if (intent.getData() == null) {
return;
}
//聚合會話引數
String type = intent.getData().getQueryParameter("type");
if (type == null)
return;
if (type.equals("group")) {
mMHeadView.setTitle("群組");
} else if (type.equals("private")) {
mMHeadView.setTitle("私人會話");
} else if (type.equals("discussion")) {
mMHeadView.setTitle("我的討論組");
} else if (type.equals("system")) {
mMHeadView.setTitle("系統訊息");
} else {
mMHeadView.setTitle("聊天");
}
}
@Override
public void initData() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
}
@Override
public void initPresenter() {
}
}
上面就算是基本的融雲IM的使用了 下面再附上一些自定義的功能
-
底部按鈕的新增 注意:需要在初始化時把自定義的按鈕新增進去
/** * 新增自定義按鈕 */ public class RecognizeExtensionModule2 extends DefaultExtensionModule { private static RecognizeExtensionModule2 singleInstance = null; private RecognizeExtensionModule2() { } public static RecognizeExtensionModule2 getInstence(){ if (singleInstance == null) { synchronized (RecognizeExtensionModule.class) { if (singleInstance == null) { singleInstance = new RecognizeExtensionModule2(); } } } return singleInstance; } @Override public void onInit(String appKey) { } @Override public void onConnect(String token) { } @Override public void onAttachedToExtension(RongExtension extension) { } @Override public void onDetachedFromExtension() { } @Override public void onReceivedMessage(Message message) { } @Override public List<IPluginModule> getPluginModules(Conversation.ConversationType conversationType) { List<IPluginModule> pluginModules = new ArrayList<>(); //建立位置按鈕 LocationPlugin recognizePlugin = new LocationPlugin(); //新增位置 pluginModules.add(recognizePlugin); //自定義的按鈕 SignInPlugin signInPlugin = new SignInPlugin(); if (conversationType.equals(Conversation.ConversationType.GROUP)){ pluginModules.add(signInPlugin); } return pluginModules; } @Override public List<IEmoticonTab> getEmoticonTabs() { return super.getEmoticonTabs(); } @Override public void onDisconnect() { } }
-
按鈕的介面
/** * 傳送位置按鈕 */ public class LocationPlugin implements IPluginModule { private Conversation.ConversationType conversationType; private String targetId; @Override public Drawable obtainDrawable(Context context) { //按鈕的資源 Drawable drawable = BaseApplication.getContext().getResources().getDrawable(R.mipmap.icon_rm_postion); return drawable; } @Override public String obtainTitle(Context context) { return "位置"; //名稱 } @Override public void onClick(Fragment fragment, RongExtension rongExtension) { //示例獲取 會話型別、targetId、Context,此處可根據產品需求自定義邏輯,如:開啟新的 Activity 等。 conversationType = rongExtension.getConversationType(); targetId = rongExtension.getTargetId(); //只有通過 extension 中的 startActivityForPluginResult 才會返回到本類中的 onActivityResult rongExtension.startActivityForPluginResult(new Intent(fragment.getActivity(),BaiDuLocationActivity.class),5, this); } @Override public void onActivityResult(int i, int i1, Intent data) { if (data != null) { double latitude = data.getDoubleExtra("latitude",0.00); double longitude = data.getDoubleExtra("longitude",0.00); String address = data.getStringExtra("address"); String uri = data.getStringExtra("locuri"); //傳送位置訊息 LocationMessage locationMessage = LocationMessage.obtain(latitude,longitude,address, Uri.parse(uri)); RongIM.getInstance().sendLocationMessage(Message.obtain(targetId, conversationType, locationMessage), null, null, null); } } }
-
新增融雲客服功能
CSCustomServiceInfo.Builder csBuilder = new CSCustomServiceInfo.Builder(); CSCustomServiceInfo csInfo = csBuilder.nickName("融雲").build(); RongIM.getInstance().startCustomerServiceChat(getActivity(), "key", "name", csInfo);
-
點選融雲推送的聊天訊息 需要在配置檔案中配置
public class SealNotificationReceiver extends PushMessageReceiver { /* push 通知到達事件*/ @Override public boolean onNotificationMessageArrived(Context context, PushNotificationMessage message) { return false; // 返回 false, 會彈出融雲 SDK 預設通知; 返回 true, 融雲 SDK 不會彈通知, 通知需要由您自定義。 } /* push 通知點選事件 */ @Override public boolean onNotificationMessageClicked(Context context, PushNotificationMessage message) { return false; // 返回 false, 會走融雲 SDK 預設處理邏輯, 即點選該通知會開啟會話列表或會話介面; 返回 true, 則由您自定義處理邏輯。 } }
以上就是對專案融雲的一個簡單總結 各位僅做參考 還有許多融雲功能未用到,有需要的朋友可以去參考融雲的官方文件以及下載推薦的Dome研究