Instant Messaging (im-instant messager)

Source: Internet
Author: User

Instant Messaging is also called real-time communication, which simply means that two or more people use the network for textfileVoiceAnd Videoof communication.

First, the network to communicate, it is necessary to network protocol, Instant Messaging protocol is XMPP. The message type to be passed by the XMPP protocol is in XML format.
The implementation class of the HTTP protocol is the implementation class of the SOCKET,XMPP protocol called Smack.

Second, instant Messaging definitely requires server-side (OpenFire) and Client (Spark).
The relationship between Openfire,spark and Smack is as follows:

There are four ways to send messages in instant Messaging:
-Instant online communication: two people online
-Instant Proxy communication: Two people are online, but because the network or other problem messages are not sent, the message is sent to the server first, and then the server forwards to the other side
-Offline communication: The other side is not on the line, the server is staged, the other side online and then sent to each other
-Other ways: the other person is not on the line, by SMS or email to inform the other party

The following three-party SDK based on the ring letter, a brief development.

1. Get Appkey in the official website

Fill in the application name, description, package name and other information to get Appkey

2. Server-side

The server side is not introduced here ...

3. Client:
    1. Download and import the SDK
      There will be a demo in the SDK that can be run directly to achieve instant messaging functionality, but the code will be integrated into
    2. Configuration information
      Add the following permissions to the manifest file Androidmanifest.xml and write your registered Appkey

Permission configuration (additional permissions may be required in the actual development, refer to Demo):

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Your package "android:versioncode=" Android:versionname= "1.0.0" > <!--Required-< Uses-permission android:name= "Android.permission.VIBRATE"/> <uses-permission android:name= " Android.permission.INTERNET "/> <uses-permission android:name=" Android.permission.RECORD_AUDIO "/> <use S-permission android:name= "Android.permission.CAMERA"/> <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/> <uses-permission android:name=" android.permission.ACCESS_MOCK_ Location "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses-permiss Ion Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name= " Android.permission.ACCESS_FINE_LOCATION "/> <uses-permission android:name=" Android.permission.GET_TASKS "/> <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE "/> <uses-permission Android : Name= "Android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name= "Android.permission.WAKE_LOCK "/> <uses-permission android:name=" Android.permission.MODIFY_AUDIO_SETTINGS "/> <uses-permission androi D:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name= "android.permission.RECEIVE_ Boot_completed "/> <application android:icon=" @drawable/ic_launcher "android:label=" @string/app_nam E "android:name=" Your Application "> <!--Set the Appkey--and <meta-data android:name= of the ring-letter app Easem Ob_appkey "android:value=" Your APPKEY "/> <!--Declaration SDK required Service SDK core features--<service android:name = "Com.hyphenate.chat.EMChatService" android:exported= "true"/> <!--claims that the SDK requires--and <receive R Android:name= "Com.hyphenate.chat.EMMonitorreceiver "> <intent-filter> <action android:name=" Android.intent.action.PACKAG  E_removed "/> <data android:scheme=" package "/> </intent-filter> <!-- Optional Filter-<intent-filter> <action android:name= "Android.intent.action.BOOT_COMPL Eted "/> <action android:name=" Android.intent.action.USER_PRESENT "/> </intent-filter > </receiver> </application></manifest>
    1. App Packaging obfuscation

Add the following keep to the Proguard file

-keep class com.hyphenate.** {*;}-dontwarn  com.hyphenate.**
Send Message

The configuration is complete to send the message.

1. Sending text messages
//创建一条文本消息,content为消息文字内容,toChatUsername为对方用户或者群聊的id,后文皆是如此EMMessage message = EMMessage.createTxtSendMessage(content, toChatUsername);//如果是群聊,设置chattype,默认是单聊message.setChatType(ChatType.GroupChat);//发送消息EMClient.getInstance().chatManager().sendMessage(message);
2. Send voice messages
//filePath为语音文件路径,length为录音时间(秒)EMMessage message = EMMessage.createVoiceSendMessage(filePath, length, toChatUsername);//如果是群聊,设置chattype,默认是单聊message.setChatType(ChatType.GroupChat);EMClient.getInstance().chatManager().sendMessage(message);
3. Send a video message
//videoPath为视频本地路径,thumbPath为视频预览图路径,videoLength为视频时间长度EMMessage message = EMMessage.createVideoSendMessage(videoPath, thumbPath, videoLength, toChatUsername);//如果是群聊,设置chattype,默认是单聊message.setChatType(ChatType.GroupChat);EMClient.getInstance().chatManager().sendMessage(message);
4. Send Picture message
//imagePath为图片本地路径,false为不发送原图(默认超过100k的图片会压缩后发给对方),需要发送原图传falseEMMessage.createImageSendMessage(imagePath, false, toChatUsername);//如果是群聊,设置chattype,默认是单聊message.setChatType(ChatType.GroupChat);EMClient.getInstance().chatManager().sendMessage(message);
Receiving messages

Receive messages by registering for message snooping

EMClient.getInstance().chatManager().addMessageListener(msgListener);EMMessageListener msgListener = new EMMessageListener() {    @Override    public void onMessageReceived(List<EMMessage> messages) {        //收到消息    }    @Override    public void onCmdMessageReceived(List<EMMessage> messages) {        //收到透传消息    }    @Override    public void onMessageReadAckReceived(List<EMMessage> messages) {        //收到已读回执    }    @Override    public void onMessageDeliveryAckReceived(List<EMMessage> message) {        //收到已送达回执    }    @Override    public void onMessageChanged(EMMessage message, Object change) {        //消息状态变动    }};

Remember to remove listener when you do not need it, as in the activity's OnDestroy ()
EMClient.getInstance().chatManager().removeMessageListener(msgListener);

Instant Messaging (im-instant messager)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.