Skip to main content

Overview

The Chat API provides real-time messaging functionality using Firebase Realtime Database, enabling seamless communication between managers and salespersons in the Sales Management App.

ChatRoom

Activity that manages the chat room interface for viewing and sending messages. Package: project.avishkar.salesmanagement.Chat

Key Features

  • Real-time message synchronization
  • Firebase integration for persistent messaging
  • RecyclerView-based message display
  • Automatic message list updates

Properties

recyclerView
RecyclerView
Displays the list of chat messages
messageListAdapter
ChatRoomAdapter
Adapter for rendering messages in the RecyclerView
sendButton
Button
Button to send messages
chatBox
EditText
Input field for composing messages
SalespersonName
String
Name of the salesperson in the chat
ManagerNumber
String
Manager’s unique identifier for the chat room

Methods

onCreate()

Initializes the chat room activity and sets up Firebase listeners.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_personal_chat);
    
    recyclerView = findViewById(R.id.recylerview_message_list);
    sendButton = findViewById(R.id.button_chatbox_send);
    chatBox = findViewById(R.id.edittext_chatbox);
    
    databaseReference = FirebaseDatabase.getInstance()
        .getReference("ChatRoom");
}
The timestamp is calculated using System.currentTimeMillis() / 1000 - 19800 to adjust for timezone offset.

PersonalChatActivityManager

Activity for managers to engage in one-on-one chat with salespersons. Package: project.avishkar.salesmanagement.Chat

Key Features

  • Personal messaging between manager and salesperson
  • Smooth scrolling to latest messages
  • Real-time message updates
  • Session management integration

Properties

recyclerView
RecyclerView
Displays the message list
messageListAdapter
MessageListAdapter
Adapter for message rendering
SalespersonName
String
Name of the salesperson
ManagerName
String
Name of the manager
tmp1
String
Sanitized manager name (whitespace removed)
tmp2
String
Sanitized salesperson name (whitespace removed)

Implementation Details

// Chat room identifier format
String chatKey = tmp1 + "-" + tmp2;
// Example: "JohnManager-JaneSalesperson"

databaseReference.child(chatKey).addChildEventListener(...);
The activity automatically scrolls to the latest message using recyclerView.smoothScrollToPosition(mMessages.size()-1)

PersonalChatActivitySalesperson

Activity for salespersons to chat with their assigned manager. Package: project.avishkar.salesmanagement.Chat

Key Features

  • Salesperson-side personal messaging
  • Firebase real-time synchronization
  • Message validation
  • Session-aware messaging

Properties

recyclerView
RecyclerView
Displays the conversation messages
messageListAdapter
MessageListAdapter
Handles message list rendering
sendButton
Button
Triggers message sending
chatBox
EditText
Text input for message composition
databaseReference
DatabaseReference
Firebase reference to ChatsTable

Message Structure

Messages are stored using the BaseMessage model:
public class BaseMessage {
    String message;      // Message content
    String senderName;   // Sender's name
    String msgRole;      // Sender's role (manager/salesperson)
    String createdAt;    // Timestamp
    
    public BaseMessage(String message, String createdAt, 
                      String senderName, String msgRole) {
        this.message = message;
        this.senderName = senderName;
        this.createdAt = createdAt;
        this.msgRole = msgRole;
    }
}

Validation

The activity includes message validation:
if (!message.equals("")) {
    // Send message
    BaseMessage baseMessage = new BaseMessage(
        message, 
        ts, 
        SalespersonName, 
        mp.get("role")
    );
    // Push to Firebase
} else {
    Toast.makeText(
        getApplicationContext(), 
        "message is empty!!", 
        Toast.LENGTH_SHORT
    ).show();
}

Firebase Structure

The chat system uses two main Firebase paths:
  • ChatRoom: For group chat rooms identified by manager number
  • ChatsTable: For personal one-on-one conversations identified by ManagerName-SalespersonName
All chat activities use SessionManager to retrieve current user details including ID and role.

Build docs developers (and LLMs) love