Skip to main content

Overview

The MessageManager class handles all messaging operations in Threadly, including sending messages, retrieving conversations, uploading media in messages, managing message status, and handling message deletion. All methods are static for convenient access throughout the application.

Methods

sendMessage

Sends a message to another user.
public static void sendMessage(JSONObject object)
object
JSONObject
required
Message object containing sender, receiver, content, and metadata
The JSONObject should include:
  • senderUUId (String) - Sender’s unique identifier
  • recieverUUId (String) - Receiver’s unique identifier
  • message (String) - Message content
  • type (String) - Message type (text, image, video, etc.)
  • MsgUid (String) - Unique message identifier

Example

JSONObject messageData = new JSONObject();
messageData.put("senderUUId", "sender-uuid-123");
messageData.put("recieverUUId", "receiver-uuid-456");
messageData.put("message", "Hello! How are you?");
messageData.put("type", "text");
messageData.put("MsgUid", UUID.randomUUID().toString());

MessageManager.sendMessage(messageData);

getaAndUpdatePendingMessagesFromServer

Retrieves and stores pending messages from a specific sender.
public static void getaAndUpdatePendingMessagesFromServer(String senderUUid)
senderUUid
String
required
UUID of the sender whose messages to retrieve
This method:
  • Fetches pending messages from the server
  • Stores them in the local database
  • Updates conversation history
  • Automatically handles message synchronization

Example

MessageManager.getaAndUpdatePendingMessagesFromServer("sender-uuid-123");

checkAndGetPendingMessages

Checks for pending messages from all conversations and retrieves them.
public static void checkAndGetPendingMessages()
This method:
  • Checks all active conversations for new messages
  • Automatically fetches pending messages
  • Updates local storage
  • Should be called periodically or on app resume

Example

// Call when app resumes or periodically
MessageManager.checkAndGetPendingMessages();

setSeenMessage

Marks messages as seen/read.
public static void setSeenMessage(
    List<String> MessageUids,
    String senderUUid,
    String receiverUUid,
    NetworkCallbackInterface callbackInterface
) throws JSONException
MessageUids
List<String>
required
List of message UIDs to mark as seen
senderUUid
String
required
UUID of the message sender
receiverUUid
String
required
UUID of the message receiver (current user)
callbackInterface
NetworkCallbackInterface
required
Callback interface to handle the response

Example

List<String> messageIds = Arrays.asList("msg-uid-1", "msg-uid-2", "msg-uid-3");

try {
    MessageManager.setSeenMessage(
        messageIds,
        "sender-uuid-123",
        "receiver-uuid-456",
        new NetworkCallbackInterface() {
            @Override
            public void onSuccess() {
                // Messages marked as seen
            }
            
            @Override
            public void onError(String err) {
                // Handle error
            }
        });
} catch (JSONException e) {
    // Handle exception
}

UploadMsgMedia

Uploads media files (images, videos) to be sent in messages.
public static void UploadMsgMedia(
    File filepath,
    String Tag,
    NetworkCallbackInterfaceWithProgressTracking callbackInterfaceWithProgressTracking
)
filepath
File
required
Media file to upload
Tag
String
required
Unique tag to identify and potentially cancel the upload
callbackInterfaceWithProgressTracking
NetworkCallbackInterfaceWithProgressTracking
required
Callback interface to handle upload progress and response
response
JSONObject
Contains the uploaded media URL and metadata

Example

File mediaFile = new File("/path/to/photo.jpg");
String uploadTag = "upload-" + System.currentTimeMillis();

MessageManager.UploadMsgMedia(
    mediaFile,
    uploadTag,
    new NetworkCallbackInterfaceWithProgressTracking() {
        @Override
        public void progress(long bytesUploaded, long totalBytes) {
            int percentage = (int) ((bytesUploaded * 100) / totalBytes);
            // Update progress UI
        }
        
        @Override
        public void onSuccess(JSONObject response) {
            String mediaUrl = response.optString("url");
            // Send message with media URL
        }
        
        @Override
        public void onError(String error) {
            // Handle error
        }
    });

CancelMessageMediaUploadRequest

Cancels an ongoing media upload.
public static void CancelMessageMediaUploadRequest(String Tag)
Tag
String
required
Tag of the upload request to cancel (same tag used in UploadMsgMedia)

Example

String uploadTag = "upload-123456789";
// User cancels the upload
MessageManager.CancelMessageMediaUploadRequest(uploadTag);

GetAllChatsAssociatedWithUser

Retrieves all chat conversations for the current user.
public static void GetAllChatsAssociatedWithUser(
    NetworkCallbackInterfaceWithJsonObjectDelivery callback
)
callback
NetworkCallbackInterfaceWithJsonObjectDelivery
required
Callback interface to handle the response
response
JSONObject
Array of conversation objects with last message, participant info, and unread count

Example

MessageManager.GetAllChatsAssociatedWithUser(
    new NetworkCallbackInterfaceWithJsonObjectDelivery() {
        @Override
        public void onSuccess(JSONObject response) {
            JSONArray chats = response.optJSONArray("chats");
            // Display chat list
        }
        
        @Override
        public void onError(String error) {
            // Handle error
        }
    });

DeleteMessageForLoggedInUser

Deletes a message for the current user only (other participant can still see it).
public static void DeleteMessageForLoggedInUser(
    String MsgUid,
    String Role,
    NetworkCallbackInterface callbackInterface
)
MsgUid
String
required
Unique identifier of the message to delete
Role
String
required
User’s role in the conversation (“sender” or “receiver”)
callbackInterface
NetworkCallbackInterface
required
Callback interface to handle the response

Example

MessageManager.DeleteMessageForLoggedInUser(
    "msg-uid-123",
    "sender",
    new NetworkCallbackInterface() {
        @Override
        public void onSuccess() {
            // Message deleted for user
        }
        
        @Override
        public void onError(String err) {
            // Handle error
        }
    });

unSendMessage

Unsends a message, removing it for both sender and receiver.
public static void unSendMessage(
    String messageUid,
    String receiverUUid,
    NetworkCallbackInterface callbackInterface
)
messageUid
String
required
Unique identifier of the message to unsend
receiverUUid
String
required
UUID of the message receiver
callbackInterface
NetworkCallbackInterface
required
Callback interface to handle the response

Example

MessageManager.unSendMessage(
    "msg-uid-123",
    "receiver-uuid-456",
    new NetworkCallbackInterface() {
        @Override
        public void onSuccess() {
            // Message unsent successfully
        }
        
        @Override
        public void onError(String err) {
            // Handle error
        }
    });

Message Model Structure

Messages follow the structure defined in MessageSchema.java:8:
  • messageUid (String) - Unique message identifier
  • conversationId (String) - Conversation identifier
  • replyToMsgId (String) - ID of message being replied to (if any)
  • senderId (String) - Sender’s UUID
  • receiverId (String) - Receiver’s UUID
  • msg (String) - Message content
  • type (String) - Message type (text, image, video, post)
  • timestamp (String) - Message timestamp in ISO 8601 format
  • deliveryStatus (int) - Delivery status code (0=pending, 1=sent, 2=delivered, 3=seen)
  • isDeleted (boolean) - Whether message is deleted
  • postId (int) - ID of shared post (if type is post)
  • postLink (String) - Link to shared post

Delivery Status Codes

  • 0 - Pending (queued for sending)
  • 1 - Sent (delivered to server)
  • 2 - Delivered (received by recipient’s device)
  • 3 - Seen (read by recipient)

API Endpoints Used

  • POST /api/messages/send - Send message
  • POST /api/messages/pending - Get pending messages from sender
  • GET /api/messages/check-pending - Check for pending messages
  • POST /api/messages/seen - Mark messages as seen
  • POST /api/messages/upload-media - Upload message media
  • GET /api/messages/chats - Get all chats
  • PATCH /api/messages/delete - Delete message for user
  • PATCH /api/messages/unsend - Unsend message
  • ProfileManager - User profile information
  • PostsManager - Share posts in messages
  • See MessageManager.java:34 for implementation details

Build docs developers (and LLMs) love