Allow users to initiate a private one-on-one chat from another user’s profile or list screen using CometChat’s Android UI Kit.

Overview

The Message Privately feature streamlines direct messaging by enabling:
  • Quick entry into a one-on-one conversation from a user context.
  • Automatic conversation creation if none exists.
  • Optional initial message send to surface the chat in lists.
Users tap Message Privately → launch MessagesActivity with the target user’s UID → chat UI loads.

Prerequisites

  • Android Studio project with cometchat-pro/android-chat-sdk v4.0.0+ and cometchat-pro/android-java-uikit v4.0.0+ in build.gradle.
  • Valid CometChat App ID, Auth Key, and Region initialized.
  • <uses-permission android:name="android.permission.INTERNET"/> in AndroidManifest.xml.
  • Users created in your CometChat app.
  • User must be logged in via CometChatUIKit.login() before invoking this feature.
  • Existing MessagesActivity capable of handling one-on-one chats.

Components

Component / ClassResponsibility
UserDetailsActivityShows user profile UI and Message Privately button.
MessagesActivityChat screen for one-on-one conversation with a User.
MainActivity(Optional) entry point for sending initial message programmatically.
CometChatUIKit.login()Authenticates the current user session.
CometChat.sendMessage()Sends a dummy text message to initialize conversation.

Integration Steps

1. Launch One-on-One Chat from Profile

Navigate from UserDetailsActivity to MessagesActivity with the selected user.
// In UserDetailsActivity.java
binding.messagePrivatelyBtn.setOnClickListener(v -> {
  Intent intent = new Intent(this, MessagesActivity.class);
  intent.putExtra(getString(R.string.app_user), new Gson().toJson(user));
  startActivity(intent);
});
File reference:
UserDetailsActivity.java

2. Handle Incoming Intent in Chat Screen

Deserialize the User JSON extra and configure the chat UI.
// In MessagesActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_messages);

  String userJson = getIntent().getStringExtra(getString(R.string.app_user));
  if (userJson != null) {
    User user = new Gson().fromJson(userJson, User.class);
    messageHeader.setUser(user);
    messageList.setUser(user);
    composer.setUser(user);
  }
}
File reference:
MessagesActivity.java

3. (Optional) Programmatically Create Conversation

Send an initial greeting to ensure the chat appears in conversation lists.
// In MainActivity.java
txtMessage = new TextMessage(
  "cometchat-uid-2",
  "👋",
  CometChatConstants.RECEIVER_TYPE_USER
);
CometChat.sendMessage(txtMessage, new CometChat.CallbackListener<BaseMessage>() {
  @Override public void onSuccess(BaseMessage msg) {
    Log.d("SendMsg", "Initial message sent.");
  }
  @Override public void onError(CometChatException e) {
    Log.e("SendMsg", e.getMessage());
  }
});
File reference:
MainActivity.java

Implementation Flow

StepActionLocation
1Tap Message Privately buttonUserDetailsActivity.java
2Launch MessagesActivity with user JSON extraUserDetailsActivity onClick listener
3Deserialize and bind User to UI Kit componentsMessagesActivity.onCreate()
4(Optional) Send initial message to surface chat listMainActivity.sendInitialMessage()

Customization Options

  • Button Text & Style: Update messagePrivatelyBtn in activity_user_details.xml.
  • Intent Extras Key: Use a custom key instead of R.string.app_user.
  • Dummy Message: Customize initial message content or omit step if undesired.

Edge Cases

ConditionBehavior
User never chatted beforeThe conversation appears after the first message is sent.
Target user blockedComposer hidden; consider showing unblock prompt.
Invalid user dataGuard against null JSON and show error UI or fallback.
Missing JSON extraDo not initialize chat; optionally close screen with warning.

Summary / Feature Matrix

FeatureComponent / Method
Launch private chatIntent from messagePrivatelyBtn
Initialize chat UImessageHeader.setUser(), messageList.setUser()
Create conversationCometChat.sendMessage() optional

Android Sample App (Java)

Explore this feature in the CometChat SampleApp: GitHub → SampleApp

Android Sample App (Kotlin)

Explore this feature in the CometChat SampleApp: GitHub → SampleApp