Android融雲會話及會話列表的實現
阿新 • • 發佈:2019-02-16
一、服務端好友關係列表在自己的伺服器上已部署完成,並且實現了與融雲伺服器的互動
二、通過App傳送訊息到自己的服務端,得到App與自己伺服器互動的token以及App與融雲伺服器互動的token
1、App自己伺服器token(令牌),用於App與自己伺服器互動
2、App融雲伺服器token(令牌),使用者App與融雲伺服器互動
三、在融雲官方網站上下載IMKit、IMLib,並進行新增配置
四、一般在Application onCreate()函式中進行融雲初始化設定
public class Application extends Application implements RongIM.UserInfoProvider{@Overridepublic void onCreate() { super.onCreate(); if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext())) || "io.rong.push".equals(getCurProcessName(getApplicationContext()))) { RongIM.init(this); RongIM.setOnReceiveMessageListener(new RongMessageHandling.MyReceiveMessageListener()); RongIM.setConnectionStatusListener(new RongMessageHandling.MyConnectionStatusListener()); RongIM.getInstance().registerConversationTemplate(new RongMessageHandling.MyPrivateConversationProvider()); //RongIM.getInstance().registerMessageTemplate(new MyTextMessageItemProvider());}} /** * 獲取融雲所需使用者資訊 * */ @Override public UserInfo getUserInfo(String userId) { if (FriendInfoList.getInstance() == null) { return null; } return FriendInfoList.getInstance().getUserInfoByUserId(userId); }}
定義相應的監聽類
/** * 融雲訊息監聽 */ static public class MyReceiveMessageListener implements RongIMClient.OnReceiveMessageListener { /** * 收到訊息的處理。 * * @param message 收到的訊息實體。 * @param left 剩餘未拉取訊息數目。 * @return 收到訊息是否處理完成,true 表示自己處理鈴聲和後臺通知,false 走融雲預設處理方式。 */ @Override public boolean onReceived(Message message, int left) { switch (message.getConversationType()){ case PRIVATE: //單聊 Log.d("MyReceiveMessage", "--單聊"); break; case GROUP: //群組 Log.d("MyReceiveMessage", "--群組"); break; case DISCUSSION: //討論組 Log.d("MyReceiveMessage", "--討論組"); break; case CHATROOM: //聊天室 Log.d("MyReceiveMessage", "--聊天室"); break; case CUSTOMER_SERVICE: //客服 Log.d("MyReceiveMessage", "--客服"); break; case SYSTEM: //系統會話 Log.d("MyReceiveMessage", "--系統會話"); break; default: break; } return false; } } /** * 自定義聊天會話的模型類 */ @ConversationProviderTag(conversationType = "private", portraitPosition = 1) static public class MyPrivateConversationProvider extends PrivateConversationProvider { @Override public View newView(Context context, ViewGroup group) { return super.newView(context, group); } @Override public void bindView(View v, int position, UIConversation data) { if(data.getConversationType().equals(Conversation.ConversationType.PRIVATE)) { data.setUnreadType(UIConversation.UnreadRemindType.REMIND_ONLY); //設定會話傳送者ID、會話標題、會話頭像URL String userID = data.getConversationSenderId(); UserInfo info = FriendInfoList.getInstance().getUserInfoByUserId(userID); data.setIconUrl(info.getPortraitUri()); data.setUIConversationTitle(info.getName()); FriendInfoList.getInstance().refreshUserInfo(userID); } super.bindView(v, position, data); } }在設定了會話訊息後,需要重新整理當前的即時通訊資訊(FriendInfoList.getInstance().refreshUserInfo(userID))
RongIM.getInstance().refreshUserInfoCache(localUserInfo);五、進行App與融雲伺服器的連線(這裡的token為融雲伺服器token),這樣就建立了會話通訊的通道
//融雲連線 RongMessageHandling.connect(getApplicationContext(), MyApplication.getInstance().getRongKey(), getApplicationInfo());六、動態構建ConversationListActivity
1、activity_conversation_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/nc_bg" android:orientation="vertical"> <FrameLayout android:id="@+id/rong_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>2、ConversationListActivity.java(ConversationActivity.java同理)
public class ConversationListActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_conversation_list);
setConversationView();
} void setConversationView(){ ConversationListFragment conversationListFragment = new ConversationListFragment();Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon() .appendPath("conversationlist") //設定私聊會話,該會話聚合顯示.appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //設定系統會話,該會話非聚合顯示.appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true") .build();conversationListFragment.setUri(uri);FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.add(R.id.rong_container,conversationListFragment);transaction.commit();}}
3、Manifest設定(android:host="包名")
<!--會話列表介面--> <activity android:name=".ui.ConversationListActivity" 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="io.rong.fast" android:pathPrefix="/conversationlist/" android:scheme="rong" /> </intent-filter> </activity> <!--會話介面--> <activity android:name=".ui.ConversationActivity" 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="xxx.xxx.xxx" android:pathPrefix="/conversation/" android:scheme="rong" /> </intent-filter> </activity>七、在此,可以進行基本的會話了