Android – use Huanxin SDK to develop instant messaging function (with source code download)

In the recent project, the instant chat function is integrated to pick and choose. Finally, Huanxin SDK is selected for development. The main reason for choosing Huanxin is that the interface is convenient and concise, and the description documents are clear and easy to understand. There are Android, IOS, and background server-side documents, which are still very complete.

Huanxin official website: http://www.easemob.com/

The purpose of this article is to explain how Huanxin demo realizes instant messaging. Before integrating the Huanxin SDK into our own app, I studied the code of the Huanxin demo. After looking at it for two or three days, I basically figured out the context, but I only knew the context. When it comes to the details, I may need to further study, but that's enough. We can integrate the functions in the demo into our own app. So this article explains how to integrate Huanxin into your app.

The integration is relatively fast. The integration can be completed in a week at most. We have our own user system, so we integrate Huanxin with the existing app user system.

Before integration, you must go to the above page to understand how to integrate. Here you explain how to integrate the scheme. You need to choose this scheme according to your existing needs. I won't say much about this. I should understand it all.

Login principle

Our solution is to integrate Huanxin with the existing app user system! That is to say, our server needs to register the existing users in the Huanxin server in the background, and then automatically log in to the Huanxin server when app logs in, and then use the instant messaging function of Huanxin.

This means that when users log in to the app, they need to log in twice, one is our application server and the other is Huanxin server. It just gives the user the feeling that they have logged in once, and the login of Huanxin server is controlled in the code, and the user can't see or feel it.

Principle of friend system

After logging in, you can get friends and groups. Huanxin adds the function of chat room, which is a little similar to the function of loose group, but the chat room is more casual. Everyone in the group understands that, not to mention, chat rooms are different. Open public chat rooms allow members to enter and leave at any time. After leaving, they will no longer receive chat messages automatically.

In the friend system, Huanxin can be managed. Of course, you can also use the application server to manage your friends without using the friend management system of Huanxin. The friend management system of Huanxin is used in our project, which is mainly convenient, but it doesn't save much, because the server notifies the Huanxin server of the change of the relationship between the user system of the application server, and the Huanxin server also changes, so as to maintain the consistency between the application server and the user system of Huanxin server. Therefore, we need to consider the cost in the integration process. The main reason for using the ring letter management friend system in our project is that the app side is convenient, and the app side does not change the user system. Complex operations are implemented on the server side, so the app side is easy to implement and simple to develop.

User nickname and Avatar

The ring letter server uses a low immersion method to develop instant messaging, that is, it does not save the user's information or access the user's information, which means that the user's nickname, avatar and other information are not saved, and the developer cannot obtain the user's information through the ring letter. Therefore, Huanxin specifically provides solutions for the nickname and avatar information of users.

Method 1: obtain the nickname and avatar from the app server

Method 2: get the nickname and avatar from the message extension

The difference between method 1 and method 2 of nickname or avatar processing:

Method 1: there is no extension when sending a message. If the sender's user information does not exist locally when receiving a message, you need to query the sender's nickname and avatar URL from the app server.

Method 2: when sending a message, it is provided with a message extension containing a nickname and a avatar URL. When a message is received, it can be taken out of the message extension without going to the app server. Method 2 is compared with method 1

Advantages: the nickname is displayed immediately after receiving the message without waiting for the data returned by the app server.

Disadvantages: each message must be extended to increase the message volume. Each message has some unnecessary data.

The above are two solutions for user nicknames and avatars given by Huanxin. These two solutions can be applied at a glance. I won't say more. Mainly talk about the solution in our project. The first solution is to obtain and save the local database from the application server. After that, the query operation is the local operation. There will be a problem. What about user relationship update or information update? The main solution to this problem is that each update of the user friend system will update the user's nickname and avatar at the same time, and then update the local database to solve this problem.

Now, after these three questions are understood, we can basically start the development. You may say that instant messaging has not been explained yet? The most important thing is instant messaging. Why didn't you explain it? Don't worry about this problem. There will be problems later^_^

development

In the development process, the first thing is to study the code of Huanxin demo, which has been encapsulated. Therefore, understand the code of Huanxin demo, and the good code can be applied to the existing app.

The code of this Huanxin demo is imported into the mobile phone to run directly and register. It works very well. The code runs normally and functions normally. Therefore, after studying this code and integrating it into your own app, it's so easy!!

Several jar packages are used in the demo, mainly including Huanxin's SDK, baidu map, Youmeng data analysis, baidu map positioning, picture loading, etc. Baidu map should have nothing to say. We have integrated it in our app before, but it's a little old. This time, baidu map is also updated to the latest. At present, the latest Baidu map is very useful. It can also be regarded as a lesson, that is, to update the applied third-party jars in real time! There's nothing else to say.

The following is the subcontracting in the demo. There are many subcontracting in the demo, but the function of the code under each package can be seen from the name of the subcontracting. I mainly look at each class under the activity package, because the activity class is an interface, and others are code tool classes serving this activity class, so I mainly look at this.

There are many classes under the activity package, but we only care about a few. Chatactivity.java class is the instant chat interface, which must be integrated into our own app. The other three classes, contactlistfragment.java, chatallhistoryfragment.java and groupsactivity.java, are contact interface, reply history interface and group interface respectively. These three need to be integrated according to their own app needs. Therefore, the main research work is on these categories.

Mainactivity.java is the main interface, which integrates the above three interfaces. The main interface displays the management interface.

The rest of the work is nothing special. If you don't understand the code, you can leave me a message and communicate with each other.

In particular, mention the following classes

These categories are a little around! It was really confused at first. Now it seems that the code in the demo is also well intentioned!!

1. First look at the hxsdkhelper.java class under the controller package, and then look at the demohxsdkhelper.java class under the chatuidemo package. It is obviously an inheritance relationship! The latter is the object class used in the demo. And this parent class is obviously a control information management class under the controller package. Open this class to view the code

The function of this class can be seen from the description.

2. Look at the hxsdkmodel.java class. The name of this class is the template class, as well as the defaulthxsdkmodel.java class and demosdkmodel.java class. It is also obvious that there is an inheritance relationship. The completed functions are mainly the storage and control information display information of some instant messaging data in the app. After these categories are clear, there is basically no problem.

Explanation of main codes

1. Main class mainactivity.java

This class implements the emeventlistener interface. One method is as follows:

It mainly monitors new messages, offline messages, reply message changes, etc., and then updates the interface refreshui (). The update interface is to refresh the number of unread messages, refresh the contact list, reply list, etc.

Three listeners are registered in the initialization of the main interface, as follows:

These three listeners are listeners that monitor contact changes, group changes and link changes with the ring information server. The changes of these three listeners will call back the corresponding methods in these three listeners to facilitate developers to take corresponding measures through corresponding methods.

The code in these three listener demos is more detailed, so I won't talk about it here.

2 contact list contactlistfragment.java class

The registered listener in the human contact above uses the observer mode. First look at some code in hxsdkhelper.java

This part of the code controls observers, adding, deleting and notifying each observer. When groups, friends and blacklists are synchronized to the client through the ring letter server, notify each observer, and then refresh the UI after the observer receives them. Here is the classic application of observer mode!!! After you understand the contact list, you won't talk much about other group interfaces and reply history interfaces.

3 chat interface chatactivity.java

This class is relatively large, because the demo integrates single chat, group chat and chat room into one interface. The code is very large, but it does not affect the final integration. The function can be realized by directly integrating this class. Not much.

Attached interface:

Figure 1 reply history interface

Figure 2 contacts interface

Figure 3 setting interface

Figure 4 chat interface

Finally, attach the source code download

Supplement:

The official website of Huanxin has released im3.0 version. An app currently developed adopts im3.0 version.

The overall interface has not changed much, and the functions are the same. However, it is much more convenient to optimize the official demo code. But I have to understand the code. If you are a master, you should be able to integrate the instant messaging function of Huanxin in half a day.

The download link given in this article is im2.0. Therefore, if you want to use the im3.0 version, you need to download it from the official website.

For novices, the demo given on Huanxin's official website can be used directly. What others give is the complete app code. Novices wonder how to integrate instant messaging?

It's actually very simple!

Firstly, add the dependency package and dynamic library given by Huanxin official website to your own project. At present, the dependency packages and dynamic libraries given on the official website are divided into those with voice and video call function and those without voice and video call function. You can add it according to the functions of your app.

Then, copy the chat interface in the demo directly to your own function. At this time, a large number of errors will appear after copying it! Because the chat interface is associated with many other classes in the demo, you should copy other classes into your own project. Remember not to forget layout files, resource picture files, strings and other resource files! Suggestion: in your own project, create a new class for special release of environmental information. Because you have a lot of classes to copy! About twenty or thirty!

Finally, data is populated into the instant messaging code. There are several parts:

1) The ring letter helper class in the application class completes the initialization operation.

2) Log in to the app interface and log in to the Huanxin server. You need the login name and password to log in to Huanxin. The Huanxin login here is different from the login app. App login is the user of the application server. The user name and password are in the application server. The login ring letter is the login name and password of the ring letter, which needs to be registered with the ring letter server first. The registration operation can be done in advance on the application server. When app logs in to the application server, it can log in to the Huanxin server along with it.

3) Get friend information. It is divided into the maintenance of friend information, application server maintenance or ring letter to help you maintain. I won't say more about this. The official website of Huanxin has instructions.

4) Maintain friends list and chat message list locally. The chat information list in the ring letter has not been edited and changed by developers. This function has been integrated into the dependent ring letter package. The friends list gives a simple data table in the demo. Developers can develop and expand according to app needs.

5) Exit app. When exiting the app, be sure to call the logout method of the helper class. In this way, the previously logged in user will exit the server of Huanxin from the app. Developers should note that when I say exit here, it refers to the manual exit of APP users. It is not the user's pressing the return button of the mobile phone or the return to the main interface button that causes the app to exit. Instead, it is the exit button in the app. The currently logged in user exits the app. If the user presses the return button or return to the main interface button to return to the mobile phone desktop, it is not necessary to call the logout method of the helper class.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>