Adding instant messaging to the application requires a lot of resources to write it yourself. Inheriting a third party is a quicker way. The third party includes Rongyun, Huanxin, and Netease Yunxin.
Recently integrated Rongyun's sdk,
Install the official instructions to introduce the jar package
1. Initialize SDK
RongIM.class is the Rongyun message startup class
RongCloudEvent.class is the message push class
If message push is required, both of these must be initialized:
Initialize in Application class
/** * Initialize Rongyun sdk */ private void initRongyunSdk() { /** * Notice: * * IMKit SDK calls the first step of initialization * * context context * * Only two processes need to be initialized, the main process and the push process */ if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext())) || "io.rong.push".equals(getCurProcessName(getApplicationContext()))) { RongIM.init(this);//Main process initialization /** * Rongyun SDK event monitoring and processing * * Register related code, only need to do it in the main process. */ if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) { RongCloudEvent.init(this); //Message push initialization DemoContext.init(this); Thread.setDefaultUncaughtExceptionHandler(new RongExceptionHandler(this)); try { RongIM.registerMessageType(AgreedFriendRequestMessage.class); RongIM.registerMessageTemplate(new ContactNotificationMessageProvider()); RongIM.registerMessageTemplate(new RealTimeLocationMessageProvider()); //@ message template display RongContext.getInstance().registerConversationTemplate(new NewDiscussionConversationProvider()); } catch (Exception e) { e.printStackTrace (); } } } }
2. Group chat:
Start a group chat:
if ( RongIM.getInstance()!=null) { // start chat RongIM.getInstance().startGroupChat(context, groupId, "Group Chat"); // Single chat RongIM.getInstance().startConversation(context, Conversation.ConversationType.PRIVATE, "9527", "chat title"); // Group chat RongIM.getInstance().startPrivateChat(context, useridd, "chat title"); }else{ ToastUtils.show(context,"rongim is empty"); }In the group chat interface, according to the documentation, create a ConversationActivity and configure it
Group chat renderings:
3. Message list:
Can be integrated dynamically or statically
static integration
Put it into the conversationlistactivity for static integration, and put it into the acticity to configure it in the manifest file
Set to non-aggregated display, otherwise set the activity of aggregated display
public class ConversationListActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.conversation_list); enterFragment(); } /** * Load session list ConversationListFragment */ private void enterFragment() { ConversationListFragment fragment = (ConversationListFragment) getSupportFragmentManager().findFragmentById(R.id.list_conversation); Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon() .appendPath("conversationlist") .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //Set the non-aggregated display of private chat sessions .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//Set group session non-aggregated display .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//Set discussion group session non-aggregated display .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//Set system session non-aggregated display .build(); fragment.setUri (uri); } }
Static integration:
There is no need to configure in the manifest, please refer to the video explanation of the list and interface implementation of Huirongyun:
http://www.rongcloud.cn/docs/android_video_tutorials.html
Dynamically get the session list fragment
/** * Fragment of session list */ private Fragment mConversationFragment = null; /** * Initialize session list * @return session list */ private Fragment initConversationList(){ if (mConversationFragment == null) { ConversationListFragment listFragment = ConversationListFragment.getInstance (); Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon() .appendPath("conversationlist") .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //Set whether the private chat session is aggregated .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//群组 .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//Discussion group .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//Public service number .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//Subscription number .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//系统 .build(); listFragment.setUs (s); return listFragment; } else { return mConversationFragment; } }
Message list renderings:
4. Push class:
Rongyun's message push class should be added to the program:
public final class RongCloudEvent implements RongIMClient.OnReceiveMessageListener, RongIM.OnSendMessageListener, RongIM.UserInfoProvider, RongIM.GroupInfoProvider, RongIM.ConversationBehaviorListener, RongIMClient.ConnectionStatusListener, RongIM.LocationProvider, RongIMClient.OnReceivePushMessageListener, RongIM.ConversationListBehaviorListener, ApiCallback, Handler.Callback, RongIM.GroupUserInfoProvider { //implementation code }Push renderings:
5. Message guidance
The message shows:
Implement setOnReceiveUnreadCountChangedListener listener in the control class:
/** * Rongyun message reception, and initialization */ private void initRongMessage() { final Conversation.ConversationType[] conversationTypes = {Conversation.ConversationType.PRIVATE, Conversation.ConversationType.DISCUSSION, Conversation.ConversationType.GROUP, Conversation.ConversationType.SYSTEM, Conversation.ConversationType.PUBLIC_SERVICE, Conversation.ConversationType.APP_PUBLIC_SERVICE}; Handler handler = new Handler (); handler.postDelayed(new Runnable() { @Override public void run() { RongIM.getInstance().setOnReceiveUnreadCountChangedListener(mCountListener, conversationTypes); // RongIM.getInstance().setOnReceiveUnreadCountChangedListener(mCountListener1, Conversation.ConversationType.APP_PUBLIC_SERVICE); } }, 500); }Control the display of messages, the change and hiding of message data
public RongIM.OnReceiveUnreadCountChangedListener mCountListener = new RongIM.OnReceiveUnreadCountChangedListener() { @Override public void onMessageIncreased(int count) { if (count == 0) { mUnreadNumView.setVisibility(View.GONE); } else if (count > 0 && count < 100) { mUnreadNumView.setVisibility(View.VISIBLE); mUnreadNumView.setText(count + ""); } else { mUnreadNumView.setVisibility(View.VISIBLE); mUnreadNumView.setText(R.string.no_read_message); } } };Display message renderings:
No news:
Got a message:
Message data changes:
6. Member information: avatar and nickname
/** * Provider of user information: the callback method of GetUserInfoProvider to obtain user information. * * @param userId UserId. * @return user information, (Note: user information is provided by the developer). */ @Override public UserInfo getUserInfo(String userId) { if (userId == null) return null; if (DemoContext.getInstance() == null) return null; UserInfos userInfo = DemoContext.getInstance().getUserInfosById(userId); if (userInfo == null) { getUserInfoByUserIdHttpRequest = DemoContext.getInstance().getDemoApi().getUserInfoByUserId(userId, (ApiCallback<User>) this); } final UserInfo userInfoById = DemoContext.getInstance().getUserInfoById(userId); return userInfoById; /* String url="http://img01.taopic.com/141012/235112-1410120K20374.jpg"; // final String uriString = "http://img1.2345.com/duoteimg/qqTxImg/2013/04/22/13667759472.jpg"; String uri = "http://dev1.mobile.xiebao.me/attachment/download?username=888880&sid=0ah1i8he53pfe8fie5bh6f09h5&file_id=7845&assoc_type=agreelogo1&type=short"; UserInfo user= new UserInfo(userId, "xiaowang", Uri.parse(uri)); Log.v(tag,"Set user avatar, id = "+userId); return user;*/ }Effect picture: