20 lines of code add chat function to the App, and fast app

Source: Internet
Author: User

20 lines of code add a chat function to the app, and the app

Many apps now need to integrate IM functions, today I will share with you the steps to integrate IM basic functions. This article takes JMessage as an example.
Aurora IM (JMessage) = Aurora Push (JPush) + IM, this article only discusses the IM part, provides a concise example for everyone to quickly integrate IM functions.

Let's first look at the correspondence between the basic functions of IM and the content of this article:

line 0: Preparation
Download SDK
Integrated SDK
line 1: Introduce header files
#import <JMessage / JMessage.h>
JMessage core header file. This is the only header file that needs to be imported into your project. It refers to the header files that are needed internally.

line 2: enable event monitoring
[JMessage addDelegate: self withConversation: nil];
Used to monitor various global events
It is recommended to write before line 3
Parameters:
delegate: fill in the object needs to implement <JMessageDelegate>
conversation: nil listens to all notifications, non-nil listens to the specified conversation
line 3: Start SDK
[JMessage setupJMessage: launchOptions
                 appKey: @ "your appkey"
                channel: @ "channel name"
       apsForProduction: NO
               category: nil];
It is recommended to write in application: didFinishLaunchingWithOptions:
Parameters:
launchOptions: parameters of the launch function launchingOption
appKey: Obtaining method line 0-Integrated SDK
channel: the channel name of the application
isProduction: whether it is a production mode
category: iOS8 new notification shortcut button parameters
line 4: Register a new user
[JMSGUser registerWithUsername: @ "username"
                      password: @ "password"
             completionHandler: ^ (id resultObject, NSError * error) {
}];
Parameters:

username: username
password: password
handler: Called successfully when error is nil (the same applies below)
line 5: login
[JMSGUser loginWithUsername: @ "username"
                   password: @ "password"
          completionHandler: ^ (id resultObject, NSError * error) {
}];
line 6: Create a single chat session
[JMSGConversation createSingleConversationWithUsername: @ "username"
                                     completionHandler: ^ (id resultObject, NSError * error) {
}];
Conversation is the core of the entire IM, all message behavior is based on "conversation"
If the session does not exist, a new session will be returned, and if it exists, an existing session will be returned.
Parameters:
handler: When normal return, resultObject is JMSGConversation session object
((JMSGConversation *) resultObject) .target: the target of the other party of the conversation
User object JMSGUser
Group object JMSGGroup
Related APIs:
Create a single chat cross-application session
Create a group chat session
line 7: send a text message
[(JMSGConversation *) resultObject sendTextMessage: @ "text"];
Transform line 6-resultObject in handler and send text message
Related APIs:
Send picture message
Send voice message
Send file message
Send geographic location message
line 8 ~ 12: receive text messages
-(void) onReceiveMessage: (JMSGMessage *) message error: (NSError *) error {
    if (message.content == kJMSGContentTypeText) {
        NSString * text = ((JMSGTextContent *) message.content) .text;
    }
}
Added the class of <JMessageDelegate> in line 2, you can listen to this method
This method is called when the app receives (text, pictures, etc.) messages
Determine the message type according to message.contentType
Transform message.content into text content and get text text to show UI
Related APIs:
Picture content
Sound content
document content
line 13: Get historical news
NSArray * messages = [(JMSGConversation *) resultObject messageArrayFromNewestWithOffset: nil limit: nil];
Use the resultObject in line 6 to get it after transformation
Single chat or group chat
Parameters:
Return NSArray <JMSGMessage *>
offset: starting point. nil starts from the latest one, and n traces from the latest n to the history
limit: quantity. nil table all
line 14 ~ 15: clear the number of unread messages in single chat
JMSGConversation * conversation = [JMSGConversation singleConversationWithUsername: username];
[conversation clearUnreadCount];
Related APIs:
Clear group chat
line 16: Get session list
[JMSGConversation allConversations: ^ (id resultObject, NSError * error) {
}];
Get all conversation lists in batch
According to the type of conversation to determine whether it is single chat or group chat
Parameters:
resultObject: NSArray <JMSGConversation *>
line 17: Delete single chat session
BOOL success = [JMSGConversation deleteSingleConversationWithUsername: username];
Related APIs:
Delete single chat cross-application session
Delete group chat session
line 18: Get user details in bulk
[JMSGUser userInfoArrayWithUsernameArray: nameArr completionHandler: ^ (id resultObject, NSError * error) {
}];
Parameters:
nameArray: NSArray <NSString *>
resultObject: NSArray <JMSGUser *>
Related APIs:
Get details of this user
Modify this user's details
Modify this user's password
line 19: Create a group and related operations
[JMSGGroup createGroupWithName: name desc: desc memberArray: members completionHandler: ^ (id resultObject, NSError * error) {
}];
Parameters:
name: group name
desc: group description
memberArray: member list, NSArray <NSString *>
resultObject: Group object JMSGGroup
Related APIs:
Get my group list
Get a list of group members
Add members
Delete member
Retreat
Get group details
Edit group details
line 20: log out
[JMSGUser logout: ^ (id resultObject, NSError * error) {
}];
So far, the basic operations of an IM are over, is it very simple?
Author: pikacode - Aurora (jpush account for the Aurora team)


Original: 20 lines of code to add chat to the app


Know the column: Aurora Daily


Related Article

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.