SDK quickstart
Instant messaging connects people wherever they are and allows them to communicate with others in real time. The Chat SDK enables you to embed real-time messaging in any app, on any device, anywhere.
This page shows a sample code to add peer-to-peer messaging into your app by using the Chat SDK for Android.
Understand the tech
The following figure shows the workflow of how clients send and receive peer-to-peer messages.
As shown in the figure, the workflow of peer-to-peer messaging is as follows:
- Clients retrieve a token from your app server.
- Client A and Client B log in to Chat.
- Client A sends a message to Client B. The message is sent to the Chat server and the server delivers the message to Client B. When Client B receives the message, the SDK triggers an event. Client B listens for the event and gets the message.
Prerequisites
In order to follow the procedure in this page, you must have the following:
- A valid Agora account
- An Agora project with an App Key that has enabled the Chat service
- An Android simulator or a physical Android device.
- Android Studio 3.6 or higher.
- Java Development Kit (JDK). You can refer to the User Guide of Android for applicable versions.
Token generation
This section introduces how to register a user at Agora Console and generate a temporary token.
1. Register a user
To register a user, do the following:
-
On the Project Management page, click Config for the project that you want to use.
-
On the Edit Project page, click Config next to Chat below Features.
-
In the left-navigation pane, select Operation Management > User and click Create User.
-
In the Create User dialog box, fill in the User ID, Nickname, and Password, and click Save to create a user.
2. Generate a user token
To ensure communication security, Agora recommends using tokens to authenticate users who log in to the Agora Chat system.
For testing purposes, Agora Console supports generating temporary tokens for Agora Chat. To generate a user token, do the following:
-
On the Project Management page, click Config for the project that you want to use.
-
On the Edit Project page, click Config next to Chat below Features.
-
In the Data Center section of the Application Information page, enter the user ID in the Chat User Temp Token box and click Generate to generate a token with user privileges.
Project setup
Follow the steps to create the environment necessary to integrate Agora Chat into your app.
-
For new projects, in Android Studio, create a Phone and Tablet Android project with an Empty Activity.
After creating the project, Android Studio automatically starts gradle sync. Ensure that the sync succeeds before you continue. -
Integrate the Agora Chat SDK into your project with Maven Central.
a. In
/Gradle Scripts/build.gradle(Project: <projectname>)
, add the following lines to add the Maven Central dependency:The way to add the Maven Central dependency can be different if you set dependencyResolutionManagement in your Android project.b. In
/Gradle Scripts/build.gradle(Module: <projectname>.app)
, add the following lines to integrate the Agora Chat SDK into your Android project:minSdkVersion
must be 21 or higher for the build process to succeed.- For the latest SDK version, go to Sonatype.
-
Add permissions for network and device access.
In
/app/Manifests/AndroidManifest.xml
, add the following permissions after</application>
:These are the minimum permissions you need to add to start Agora Chat. You can also add other permissions according to your use case.
-
Prevent code obfuscation.
In
/Gradle Scripts/proguard-rules.pro
, add the following line:
This section shows how to use the Agora Chat SDK to implement peer-to-peer messaging in your app step by step.
Create the UI
-
To add the text strings used by the UI, open
app/res/values/strings.xml
and replace the content with the following codes: -
To add the UI framework, open
app/res/layout/activity_main.xml
and replace the content with the following codes:
Implement sending and receiving messages
To enable your app to send and receive messages between individual users, do the following:
-
Import classes.
Inapp/java/io.agora.agorachatquickstart/MainActivity
, add the following lines afterimport android.os.Bundle;
:
-
Define global variables.
Inapp/java/io.agora.agorachatquickstart/MainActivity
, before adding the following lines afterAppCompatActivity {
, ensure you delete theonCreate
function created by default. -
Initialize the view and the app.
Inapp/java/io.agora.agorachatquickstart/MainActivity
, add the following lines after theonCreate
function: -
Add event callbacks.
Inapp/java/io.agora.agorachatquickstart/MainActivity
, add the following lines after theinitSDK
function: -
Log in to the app.
To implement this logic, inapp/java/io.agora.agorachatquickstart/MainActivity
, add the following lines after theinitListener
function: -
Start a chat.
To enable the function of sending messages, add the following lines after thesignOut
function: -
Show logs.
To show logs, add the following lines after thesendFirstMessage
function: -
Click Sync Project with Gradle Files to sync your project. Now you are ready to test your app.
Test your app
To validate the peer-to-peer messaging you have just integrated into your app using Agora Chat, perform the following operations:
-
Log in
a. In theMainActivity
file, replace the placeholders ofUSERNAME
,TOKEN
, andAPP_KEY
with the user ID, Agora token, and App Key of the sender (Som
).
b. In Android Studio, select the device to run the project and click Run 'app'.
c. On your simulator or physical device, click SIGN IN to log in with the sender account. -
Send a message
Fill in the user ID of the receiver (Neil
) in the Enter another username box, type in the message ("How are you doing?") to send in the Enter content box, and click SEND MESSAGE to send the message. -
Log out
Click SIGN OUT to log out of the sender account. -
Receive the message
a. After signing out, change the values ofUSERNAME
,TOKEN
, andAPP_KEY
to the user Id, Agora token, and App Key of the receiver (Neil
) in theMainActivity
file.
b. Run the app on another Android device or simulator with the receiver account and receive the message "How are you doing?" sent in step 2.
Next steps
For demonstration purposes, Chat provides an app server that enables you to quickly retrieve a token using the App Key given in this guide. In a production context, the best practice is for you to deploy your own token server, use your own App Key to generate a token, and retrieve the token on the client side to log in to Agora. To see how to implement a server that generates and serves tokens on request, see Generate a User Token.
In addition to integrating the Agora Chat SDK into your project with mavenCentral, you can also manually download the Chat SDK for Android.
-
Download the latest version of the Chat SDK for Android, and extract the files from the downloaded SDK package.
-
Copy the following files or subfolders from the libs folder of the downloaded SDK to the corresponding directory of your project.
File or subfolder Path of your project agorachat_X.Y.Z.jar
~/app/libs/
/arm64-v8a/libagora-chat-sdk.so
andlibsqlite.so
~/app/src/main/jniLibs/arm64-v8a/
/armeabi-v7a/libagora-chat-sdk.so
andlibsqlite.so
~/app/src/main/jniLibs/armeabi-v7a/
/x86/libagora-chat-sdk.so
andlibsqlite.so
~/app/src/main/jniLibs/x86/
/x86_64/libagora-chat-sdk.so
andlibsqlite.so
~/app/src/main/jniLibs/x86_64/
X.Y.Z refers to the version number of the Agora Chat SDK you downloaded.
Reference
For details, see the sample code and demo app to get started with Chat.