Implementation of Android Fusion Cloud Session and Session List

1. The server-side friend relationship list has been deployed on its own server, and the interaction with the Rongyun server has been realized.

2. Send messages to your own server through the app, and get the token that the app interacts with its own server and the token that the app interacts with the Rongyun server

1. App's own server token (token), which is used for App to interact with its own server

2. App Rongyun server token (token), user App interacts with Rongyun server

3. Download IMKit and IMLib on the official website of Rongyun, and add configuration


4. Rongyun initialization settings are generally performed in the Application onCreate() function

public class Application extends Application implements RongIM.UserInfoProvider{@Override
public 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());
}}                    
    

    /**
      * Get the user information required
 by Rongyun      * */
 @Override
 public UserInfo getUserInfo (String userId) {
         if (FriendInfoList. getInstance () == null ) {
             return null;
 }
         return FriendInfoList. getInstance ().getUserInfoByUserId(userId ) ;
 }}                    

Define the corresponding listener class

/**
 * Rongyun message listener
 */
 static public class MyReceiveMessageListener implements RongIMClient.OnReceiveMessageListener {
     /**
      * Receive message processing.
     *
      * @param message The received message entity.
     * @param left     The number of remaining unpulled messages.
     * @return Whether the processing of the received message is completed, true means that the ringtone and background notification are processed by yourself, and false is the default processing method of Rongyun.
     */
 @Override
 public boolean onReceived (Message message , int left) {
         switch (message.getConversationType()){
             case PRIVATE :                   //Single chat
                 Log. d ( "MyReceiveMessage" , "--Single chat" ) ;
                 break;
             case GROUP :              //Group
                 Log. d ( "MyReceiveMessage" , "--Group " ) ;
                 break;
             case DISCUSSION :         //Discussion group
                 Log. d ( "MyReceiveMessage" , "--discussion group" ) ;
                 break;
             case CHATROOM :           // Chat room
                 Log.d( "MyReceiveMessage" , "--Chat Room" ) ;
                 break;
             case CUSTOMER_SERVICE :   //Customer Service
                 Log.d ( "MyReceiveMessage" , " --Customer Service" ) ;
                 break;
             case SYSTEM :             //System Session
 Log.d ( " MyReceiveMessage" , "--system session" ) ;
                 break;
             default :
                 break;
 }                        

        return false;
    }
}

/**
 * Custom chat session model class
 */
 @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 ) ;
 //Set session sender ID, session title, session avatar 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);
    }
}
After setting the session message, you need to refresh the current instant messaging information (FriendInfoList.getInstance().refreshUserInfo(userID))

RongIM.getInstance().refreshUserInfoCache(localUserInfo);
5. Connect the App to the Rongyun server (the token here is the Rongyun server token), thus establishing a channel for session communication

//融云连接
RongMessageHandling.connect(getApplicationContext(),
        MyApplication.getInstance().getRongKey(),
        getApplicationInfo());
6. Dynamically build 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 (similar to 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" ) //Set up private chat Session, the session aggregation displays .appendQueryParameter(Conversation.ConversationType.PRIVATE .getName () , "false" ) //Set the system session, the session non-aggregation displays .appendQueryParameter(Conversation.ConversationType.SYSTEM .getName () , "true" ) .build() ; conversationListFragment.setUs (s) ; FragmentManager fragmentManager = getSupportFragmentManager() ; FragmentTransaction transaction = fragmentManager.beginTransaction() ; transaction.add(R.id. rong_container , conversationListFragment) ; transaction.commit() ; }}

3. Manifest settings (android:host="package name")

<!--会话列表界面-->
<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>
Seven, here, you can have a basic conversation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325357594&siteId=291194637