Instant messaging connects people wherever they are and allows them to communicate with others in real time. With built-in user interfaces (UI) for the conversation list and contact list, the Agora Chat UI Samples enables you to quickly embed real-time messaging into your app without requiring extra effort on the UI.
This page shows a sample code to add peer-to-peer messaging into your app by using the Agora Chat UI Samples.
The following figure shows the workflow of how clients send and receive peer-to-peer messages:
- Clients retrieve a token from your app server.
- Client A and Client B log in to Agora Chat.
- Client A sends a message to Client B. The message is sent to the Agora 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.
- An Android simulator or a physical Android device.
- Android Studio 3.2 or higher.
- Java Development Kit (JDK). You can refer to the User Guide of Android for applicable versions.
Follow the steps to create the environment necessary to add video call 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 Chat SDK into your project with Maven Central.
/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.
/Gradle Scripts/build.gradle(Module: <projectname>.app), add the following lines to integrate the Chat UI Samples into your Android project:
Add permissions for network and device access.
/app/Manifests/AndroidManifest.xml, add the following permissions after
These are the minimum permissions you need to add to start Chat. You can also add other permissions according to your use case.
Prevent code obfuscation.
/Gradle Scripts/proguard-rules.pro, add the following line:
This section shows how to use the Chat UI Samples to implement peer-to-peer messaging in your app step by step.
To add the text strings used by the UI, open
app/res/values/strings.xmland replace the content with the following:
To add the UI framework, open
app/res/layout/activity_main.xmland replace the content with the following:
To enable your app to send and receive messages between individual users, do the following:
Implement sending and receiving messages.
app/java/io.agora.agorachatquickstart/MainActivity, replace the code with the following:
Add LogUtils and PermissionsManager.
To make troubleshooting less time-consuming, this quickstart also uses
LogUtilsclass for logs. Navigate to
app/java/io.agora.agorachatquickstart/, create a folder named
utils. In this new folder, create a
.javafile, name it
LogUtils, and copy the following codes into the file.
When your app launches, check if the permissions necessary to insert real-time chat into the app are granted. In the
utilsfile, create a
.javafile, name it
PermissionsManager, and copy the following codes into the file.
To send image and file messages, take the following configurations:
/app/src/main/res/, create a folder, name it
xml, and create an xml file named
file_paths.xml, replace the code with the following:
/app/Manifests/AndroidManifest.xml, add the following lines before
Sync Project with Gradle Filesto sync your project. Now you are ready to test your app.
To validate the peer-to-peer messaging you have just integrated into your app using Chat:
In Android Studio, click
You see the following interface on your simulator or physical device:
Create a user account and click SIGN UP. Click Sign in and you will see a log that says Sign in success.
Run the app on another Android device or simulator and create another user account. Ensure that the usernames you created are unique.
On the first device or simulator, enter the username you just created and click START CHAT. You can now start chatting between the two clients.
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 Authenticate your users with tokens.
Agora provides the fully featured AgoraChat-Starter-Kit-Android demo app as an implementation reference.
- Enable the Developer options on your Android device, and then connect it.
- Run npx react-native run-android in the project root directory.
- In Xcode, run your project.