CometChat Pro Documentation

You'll find comprehensive guides and documentation to help you start working with CometChat as quickly as possible. Let's jump right in!

Quick Start

This guide demonstrates how to add chat to an Android application using CometChat. Before you begin, we strongly recommend you read the Key Concepts guide.

I want to explore a sample app (includes UI)

Import the app into Android Studio and follow the steps mentioned in the README.md file.

Download sample app (Kotlin)

View on Github (Kotlin)

Or check our Java sample app

Get your Application Keys

Signup for CometChat and then:

  1. Create a new app
  2. Head over to the API Keys section and click on the Create API Key button
  3. Enter a name and select the scope as Auth Only
  4. Now note the API Key and App ID

Minimum Requirement

  • Android API Level 21
  • Androidx Compatibility

Add the CometChat Dependency

First, add the repository URL to the project level build.gradle file in the repositories block under the allprojects section.

allprojects {
  repositories {
    maven {
      url "https://dl.bintray.com/cometchat/pro"
    }
  }
}

Then, add the CometChat SDK to the app level build.gradle file in the dependencies section.

dependencies {
  implementation 'com.cometchat:pro-android-chat-sdk:1.8.8'
}

Then add the below lines to defaultConfig section of the app level gradle file

defaultConfig {
  ndk {
    abiFilters "armeabi-v7a", "x86"
  }
}

Finally, add the below lines android section of the app level gradle file

android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

Initialize CometChat

The init() method initializes the settings required for CometChat.

We suggest you call the init() method on app startup, preferably in the onCreate() method of the Application class.

private String appID = "APP_ID";

CometChat.init(this, appID, new CometChat.CallbackListener<String>() {
  @Override
  public void onSuccess(String successMessage) {
    Log.d(TAG, "Initialization completed successfully");
  }
  @Override
  public void onError(CometChatException e) {
    Log.d(TAG, "Initialization failed with exception: " + e.getMessage());
  }
});
 val appID:String="APP_ID"

CometChat.init(this,appID, object : CometChat.CallbackListener<String>() {
     override fun onSuccess(p0: String?) {
      Log.d(TAG, "Initialization completed successfully")
        }

      override fun onError(p0: CometChatException?) {
      Log.d(TAG, "Initialization failed with exception: " + p0?.message)
         }
        })

Make sure you replace the APP_ID with your CometChat App ID in the above code.

Login your User

Once initialization is successful, you will need to log the user into CometChat using the login() method.

We recommend you call the CometChat login method once your user logs into your app. The login method needs to be called only once.

private String UID = "SUPERHERO1";
private String apiKey = "API_KEY";

CometChat.login(UID, apiKey, new CometChat.CallbackListener<User>() {
  @Override
  public void onSuccess(User user) {
    Log.d(TAG, "Login Successful : " + user.toString());
  }
  @Override
  public void onError(CometChatException e) {
    Log.d(TAG, "Login failed with exception: " + e.getMessage());
  }
});
 val UID:String="SUPERHERO1"
 val apiKey:String="API_KEY"

CometChat.login(UID,apiKey, object : CometChat.CallbackListener<User>() {
     override fun onSuccess(p0: User?) {
         Log.d(TAG, "Login Successful : " + p0?.toString())        
      }

     override fun onError(p0: CometChatException?) {
         Log.d(TAG, "Login failed with exception: " +  p0?.message)        
      }

   })

Make sure you replace the API_KEY with your CometChat API Key in the above code.

Sample Users

We have setup 5 users for testing having UIDs: SUPERHERO1, SUPERHERO2, SUPERHERO3, SUPERHERO4 and SUPERHERO5.

The login() method returns the User object containing all the information of the logged in user.

Send a Message

Once your user has logged in, you can send a message using the sendMessage() method.

private String receiverID = "SUPERHERO2";
private String messageText = "Hello world!";
private String messageType = CometChatConstants.MESSAGE_TYPE_TEXT;
private String receiverType = CometChatConstants.RECEIVER_TYPE_USER;

TextMessage textMessage = new TextMessage(receiverID, messageText, messageType, receiverType);

CometChat.sendMessage(textMessage, new CometChat.CallbackListener<TextMessage>() {
  @Override
  public void onSuccess(TextMessage textMessage) {
    Log.d(TAG, "Message sent successfully: " + textMessage.toString());
  }
  @Override
  public void onError(CometChatException e) {
    Log.d(TAG, "Message sending failed with exception: " + e.getMessage());
  }
});
val receiverID:String="SUPERHERO2"
val messageText:String="Hello world!"
val messageType:String=CometChatConstants.MESSAGE_TYPE_TEXT
val receiverType:String=CometChatConstants.RECEIVER_TYPE_USER

val textMessage = TextMessage(receiverID, messageText, messageType,receiverType)

CometChat.sendMessage(textMessage, object : CometChat.CallbackListener<TextMessage>() {
   override fun onSuccess(p0: TextMessage?) {
      Log.d(TAG, "Message sent successfully: " + p0?.toString())               
   }

   override fun onError(p0: CometChatException?) {
      Log.d(TAG, "Message sending failed with exception: " + p0?.message)          }

})
Parameter
Description

receiverID

The UID or GUID of the recipient

text

The text message to be sent

messageType

The type of the message that needs to be sent, which in this case is text

receiverType

The type of the receiver to whom the message is to be sent i.e user or group

Once the message is sent successfully, you will receive the message information in the TextMessage object.

Receive Messages

For every activity or fragment you wish to receive messages in, you need to register the MessageListener using the addMessageListener() method.

private String listenerID = "UNIQUE_LISTENER_ID";

CometChat.addMessageListener(listenerID, new CometChat.MessageListener() {
  @Override
  public void onTextMessageReceived(TextMessage textMessage) {
    Log.d(TAG, "Message received successfully: " + textMessage.toString());
  }
});
val listenerID:String="UNIQUE_LISTENER_ID"

CometChat.addMessageListener(listenerID, object : CometChat.MessageListener() {
   override fun onTextMessageReceived(p0: TextMessage?) {
       Log.d(TAG, "Message received successfully: " + p0?.toString())        
     }
})
Parameter
Description

listenerID

An ID that uniquely identifies that listener. We recommend using the activity or fragment name


What's Next

Messaging
Calling

Quick Start


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.