android instant messaging Rongyun integrated Rongyun session interface Rongyun session list avatar and nickname settings

Recently, I wanted to do instant messaging, and I thought about Huanxin and Rongyun, so I chose Rongyun. There are some pitfalls here. I will
briefly describe the steps
. First, you need to register a Rongyun account, create a Rongyun project, configure it, get the appkey, and
download the Rongyun sdk
. Three steps to import IMKit modle and IMLib modle and associate them into the app modle
Step four configure Rongyun Appkey to configure the manifest file of IMLib (IMLib — "AndroidManifast.xml )
meta-data
android:name=”RONG_CLOUD_APP_KEY”
android:value=” Your RONGIM AppKEY” needs to configure a content support
provider
android:name=”android.support.v4.content.FileProvider”
android:authorities=”com.sxm.yiwei.FileProvider”
android:grantUriPermissions=”true”
android:exported=”false”>
meta-data
android:name=”android.support.FILE_PROVIDER_PATHS”
android:resource=”@xml/rc_file_path”/>
/provider
The basic configuration is now complete
The fifth step is to initialize RongIM. Initialize RongIM.init(this) in the oncreate() method of your application;
and then there is the dynamic acquisition of the token, which is generally required for obtaining Rongyun documents in the background. You can also look at the parameters. Just three user ids userid user name name user avatar headurl
Step 6 After obtaining the Rongyun token, we will link the Rongyun server
public void connectRong(final Context context, final String rongToken){
if (context.getApplicationInfo().packageName .equals(getCurProcessName(context))) {

        RongIM.connect(rongToken, new RongIMClient.ConnectCallback() {
// 重新获取token 
            /**
             * Token 错误。可以从下面两点检查 1.  Token 是否过期,如果过期您需要向 App Server 重新请求一个新的 Token
             *                  2.  token 对应的 appKey 和工程里设置的 appKey 是否一致
             */
            @Override
            public void onTokenIncorrect() {

                Log.d("connectrong", "onTokenIncorrect: rong--token异常");
            }

            /**
             * 连接融云成功
             * @param userid 当前 token 对应的用户 id
             *
             */
            @Override
            public void onSuccess(String userid) {
            链接成功 根据需要求去启动会话列表或者会话界面
            }

            /**
             * 连接融云失败
             * @param errorCode 错误码,可到官网 查看错误码对应的注释
             */
            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

                //排查原因 并重连

            }
        });
    }

}

public static String getCurProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}

In the seventh step, we start to open the session list. First, create an activity to inherit the AppCompatActivity xml file and call the fragment
fragment of Rongyun
android:id=”@+id/conversationlist”
android:name=”io.rong.imkit.fragment.ConversationListFragment”
android: layout_width=”match_parent”
android:layout_height=”match_parent”
is enough
and then configure the
activity in the androidManifast.xml file
android:name=”package name.Activity”
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="/conversationlist"
                android:scheme="rong" />
        /intent-filter>
    /activity>

All I need is a relatively single function, so every time I enter the session list, I will get the Rongyun token and link to the Rongyun server to ensure that the connection is normal. Also, it is to make the session list and Rongyun does not manage your user information. Therefore, the avatar and nickname of the chat object you have here may be incorrect. It may be the default avatar plus userid. Displaying here and here requires us to set up two schemes by ourselves I can only use a stupid method. Although Rongyun does not manage our users, we can get the user id list through Rongyun. Through the id, we can get the avatar and nickname from our own background and set them to Rongyun and refresh the list to display the session list correctly. The avatar and nickname of
RongIM.getInstance().getConversationList(new RongIMClient.ResultCallbackList>() {
@Override
public void onSuccess(List conversations) {
for (int i = 0; i < conversations.size(); i++) {
getListUserInfo (RongChatListActivity.this,conversations.get(i).getTargetId());
setRongUserInfo(conversations.get(i).getTargetId());
}
}

        @Override
        public void onError(RongIMClient.ErrorCode errorCode) {


        }
    });

.getConversationList This is the session list information provided by Rongyun, but basically there is only one userid that can be set by us using RongIM.setUserInfoProvider and returned to Rongyun

//设置容云用户信息
private void setRongUserInfo(final String targetid) {
    if (RongIM.getInstance()!=null){
        RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
            @Override
            public UserInfo getUserInfo(String s) {

                return null;
            }
        },true);
    }


}
下载成功 设置并刷新就可以了

UserInfo userInfo = new UserInfo(otheruserid,othernickname,Uri.parse(otheravatar));

        RongIM.getInstance().refreshUserInfoCache(userInfo);

Start the session list Map String,Boolean> map = new HashMap String, Boolean>();
map.put(Conversation.ConversationType.PRIVATE.getName(),false);//Session type
RongIM.getInstance().startConversationList(context, map);
The eighth step session interface also requires us to create an activity
xml file fragment
android:id=”@+id/conversation”
android:name=”io.rong.imkit.fragment.ConversationFragment”
android:layout_width=”match_parent”
android:layout_height=”match_parent” />
then configure the androidManifast.xml file
activity
android:name=”package name.Activity”
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>、

The Rongyun server link can be started directly.
chatid chat object id name chat object name
RongIM.getInstance().startPrivateChat(context,chatId,name);
In the conversation interface, I use the message to carry user information to set avatar and nickname

The ninth step The last step is to deal with the details. The first is the message prompt. If we are in the conversation interface or the conversation list, we can intuitively see the new message prompt. If not, then we can't see it. I can give you a prompt and put it out of the conversation list. Or manually call RongIM.disconnect() on the interface to disconnect the RongIM server by pushing to prompt a new message. When we launch the program, when we receive a message, click RongIM and directly enter the conversation interface. You will find that there is no message and you can’t This is because you don't have a link to the Rongyun server. You need to get the token in the application and connect to the Rongyun server.
Also , you need to customize a Rongyun message receiver
/**
* Author: Nade_S on 2017/12/21 .
* custom Rongyun message receiver
*/

public class MyRongReceiver extends PushMessageReceiver {
@Override
public boolean onNotificationMessageArrived(Context context, PushNotificationMessage pushNotificationMessage) {
return false;
}

@Override
public boolean onNotificationMessageClicked(Context context, PushNotificationMessage pushNotificationMessage) {

    return false;
}

}
is used to accept Rongyun push or you may not receive
androidmanifast.xml receiver configuration
receiver
android:exported=”true”
android:name=”.package name.MyRongReceiver”>
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” />
/intent-filter>
/receiver>
Well, the integration of Rongyun is completed.

Guess you like

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