Send a message

Update time: 2024/01/03 02:45:48

CommsEase Instant Messaging (IM) provides a complete set of instant messaging essential capabilities. You can build diverse features for your messaging apps using IM.

This document describes how to implement peer-to-peer (P2P) encrypted messaging messaging by calling APIs in the integrated IM SDK (NIM SDK).

  • To send and receive messages in a group chat, you must join a group first. Messaging in group chats are similar to the P2P messaging.
  • Messaging in super groups, chat rooms, and Qchat channels must be configured separately. For specific workflows, see Supergroup, Messaging in chat rooms, and Message management in Qchat

Prerequisites

  • iOS 8.0 or later.

  • Log in to the CommsEase console and complete the following operations.

    Create a project and get the AppKey and App Secret of the project. 1. In the left-side navigation pane, find Project, and click New.

    2. Input required information and click New.

    3. After creating an app, you can view the project in the left navigation bar. You can click `AppKey Management`, and get the AppKey and App Secret.
    Register IM accounts

    Register IM accounts and get accid and token for each account. The accid and token are used for log in to CommsEase server

    2. Click the specified application name in the left navigation pane to enter the details page of the application.
    2. Click Account Management in Features.

    3. On the test page, click New Account, fill in the account (accid), nickname (name), password (Token, strong) and click OK.

Workflow

Process overview

You can implement messaging for P2P chats by performing the following 4 steps.

The image below may not be displayed due to network issues. To fix the display problem, refresh the page.

uml diagram

**Step 0: Create a new project (optional).

This step creates a new project. If you already integrate NIM SDK into an existing project, ignore this ste 1. Launch Xcode and select File > New > Project in the upper left corner.
2. In the list that appears, select the iOS platform, and select App under Application.
3. Configure the new project and click Next.
Product Name and Organization Identifier are required.
4. Select the storage path and click Create to create a project.
new project

Step 1: Integrate NIM SDK

This document describes how to integrate NIM SDK by adding remote dependencies in CocoaPods. To manually integrate the SDK, see SDK integration.

  1. Compare the SDK version on SDK Download with your local repository.

    If the local repository does not contain the latest SDK, update the local repository to make sure the latest SDK is integrated.

    pod search NIMSDK_LITE //Query the NIMSDK_LITE information in the local repository.
    pod repo update // Update the local repository.
  1. Add the following code to the Podfile file in the root directory of your project.
    pod 'NIMSDK_LITE' 
  1. Run the following command to install NIM SDK.
    pod install

Step 2: Initialize NIM SDK

After integrating NIM SDK into your project, you must initialize the SDK before using the capabilities provided.

  1. Import the header file NIMSDK.h into the project file.
#import <NIMSDK/NIMSDK.h>
  1. Call the registerWithOption: method to initialize the SDK.
objc- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    //Initialize NIM SDK at startup    
    NSString *appKey        = @"your AppKey";//Assigned AppKey
    NIMSDKOption *option    = [NIMSDKOption optionWithAppKey:appKey];
    option.apnsCername      = @"your APNs cer name";//APNs certificate name
    option.pkCername        = @"your pushkit cer name";//PushKit  Push certificate name.
    [[NIMSDK sharedSDK] registerWithOption:option];
    ...
}

The previous code block provides a simplified initialization example. For more information, see Initialize the SDK.

Step 3: Log in to IM server

App users must log in to the CommsEase IM server before using the messaging feature. You can use the registered CommsEase accounts to log in to the server.

Manually log in by calling Thelogin. Sample code:

    NSString *account = @"your account";
    NSString *token   = @"your token";
    [[[NIMSDK sharedSDK] loginManager] login:account
                                    token:token
                                completion:^(NSError *error) {}];

NIM SDK can reconnect to the server when the app gets disconnected. You can also register listeners to track the login status of IM in real time. For more information, see Log in.

Step 4 P2P messaging

This section describes how to send and receive text messages between User A and User B in a P2P chat scenario using NIM SDK. For more information about messaging, see Messaging.

  1. User B calls the onRecvMessages: method in NIMChatManagerDelegate to listen for message receiving. Sample code:
- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages
{
//Receive a message.
}
  1. User A creates a text message and calls sendMessage:toSession:error: to send the mssage to User B. Sample code:
js// Send text messages 
NIMSession *session = [NIMSession session:@"user" type:NIMSessionTypeP2P];// Create a P2P session. The user parameter is the peer account.
NIMMessage *message = [[NIMMessage alloc] init];// Create a message.
message.text        = @"hello";
NSError *error = nil;// Error message
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session error:&error];// Send the message.

NIM SDK allows you to send or receive multiple types of messages, such as image, audio, video, file, location, alerts, notifications, and custom messages. For more information, see Messaging.

  1. The onRecvMessages: callback is triggered and User B receives the text message.

What’s next

To ensure communication security, replace the IM accounts you create in the CommsEase console and token for use in the testing environment with production-ready IM accounts created by calling Server APIs.

Was this page helpful?
Yes
No
  • Prerequisites
  • Workflow
  • Process overview
  • **Step 0: Create a new project (optional).
  • Step 1: Integrate NIM SDK
  • Step 2: Initialize NIM SDK
  • Step 3: Log in to IM server
  • Step 4 P2P messaging
  • What’s next