How to Determine Unread Message Counts on the Conversation and Entire Inbox

The constant isUnread, as a part of the message, holds a boolean value to indicate one of these two following cases:

isUnread = true , means that the particular message is unread isUnread = false, means that the particular message has been read by the recipient

Consider a message to be present in a conversation:

While the first unread message in a conversation is initially assigned to be null, it receives a value whenever there arrives an unread message in the conversation.

Now, the message is assigned to be the first unread message in a conversation, and ultimately, is pushed into an array.

Example:

unreadMessages.push(message) where unreadMessages is an array and message stands for a message in a conversation.

Then the length of the array is calculated and stored in unreadCount, which determines the Unread Badge on the Conversation

Example:

conversation.unreadCount = unreadMessages.length

Consider there are multiple unread messages in multiple conversations in an organization (inbox level):

Now that each conversation has its own unread count of messages, carried in conversation object, we initialize a variable, for example convUnreadCount, which keeps on incrementing its value everytime it encounters with unread count of messages in each conversation.

Example:

unreadCount += convUnreadCount

The final value of convUnreadCount then denotes the Unread Badge on the organization (inbox)