Unread messages

The Unread Message Count feature shows users how many messages they missed while offline. Once they reconnect or log into the app again, they can get the number of messages published in a given channel while they were gone.

Unity Chat SDK stores info about the timetoken of the last message the current user read on a given channel on the lastReadMessageTimetoken property which is available on the C++ parent object of the Membership object.

This property is automatically set for a user when they first join (Join()) a given channel or are invited to it (Invite() and InviteMultiple()). For all further updates of this property, you must decide what user actions will trigger methods that mark messages as read in your chat app - whether that's some user interaction on UI, like scrolling, your app going offline, or any other behavior.

Requires App Context and Message Persistence

To store message timetokens on the user-channel membership relationship at PubNub, you must enable App Context and Message Persistence for your app's keyset in the Admin Portal.

Get last read message

lastReadMessageTimetoken() lets you check what is the timetoken of the last message a user read on a given channel.

This method returns the last read message timetoken stored in the membership data between the channel and the current user. Use it to display markers on the message list denoting which messages haven't been read by the user yet.

Method signature

This method has the following signature:

membership.GetLastReadMessageTimeToken()

Input

This method doesn't take any parameters.

Output

TypeDescription
stringRetrieved timetoken of the last message a user read on a given channel.

Basic usage

Get the timetoken of the last message read by the support_agent_15 user on the support channel.

// reference the "support_agent_15" user
if (chat.TryGetUser("support_agent_15", out var user))
{
Console.WriteLine($"Found user with name {user.Name}");

// get the list of all user memberships
var membershipsResponse = user.GetMemberships();

// extract the actual memberships (support channel only) from the response
var memberships = membershipsResponse.Memberships.Where(x => x.ChannelId == "support").ToList();

// since we filtered for the "support" channel, we should find it directly
var membership = memberships.FirstOrDefault();

if (membership != null)
show all 29 lines

Get unread messages count (one channel)

GetUnreadMessagesCount() returns the number of messages you didn't read on a given channel. You can display this number on UI in the channel list of your chat app.

Method signature

This method has the following signature:

membership.GetUnreadMessagesCount()

Input

This method doesn't take any parameters.

Output

TypeDescription
intRetrieves from Message Persistence the number of all messages unread by a given user on a given channel from the last read message.

Basic usage

Get the number of all messages unread by the support_agent_15 user on the support channel.

// reference the "support_agent_15" user
if (chat.TryGetUser("support_agent_15", out var user))
{
Console.WriteLine($"Found user with name {user.Name}");

// get the list of all user memberships using the GetMemberships method
var membershipsResponse = user.GetMemberships();

// extract the actual memberships from the response
var memberships = membershipsResponse.Memberships;

// filter out the membership for the "support" channel
var membership = memberships.FirstOrDefault(m => m.ChannelId == "support");

if (membership != null)
show all 29 lines

Get unread messages count (all channels)

GetUnreadMessagesCounts() returns info on all messages you didn't read on all joined channels. You can display this number on UI in the channel list of your chat app.

Method signature

This method has the following signature:

chat.GetUnreadMessagesCounts(
string filter = "",
string sort = "",
int limit = 0,
Page page = null
)

Input

ParameterTypeRequiredDefaultDescription
filterstringNoempty stringExpression used to filter the results. Returns only these unread messages whose properties satisfy the given expression. The filter language is defined here.
sortstringNoempty stringKey-value pair of a property to sort by, and a sort direction. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. By default, the items are sorted by the last updated date.
limitnumberNo0Number of objects to return in response.
pagePageNonullObject used for pagination to define which previous or next result page you want to fetch.

Output

TypeDescription
List<UnreadMessageWrapper>A list of UnreadMessageWrapper objects containing the filtered, sorted, and paginated unread message counts.

Basic usage

Get the number of all messages unread by the current user.

// retrieve the current user
if (chat.TryGetCurrentUser(out User currentUser))
{
Console.WriteLine($"Current user is {currentUser.Name}");

// retrieve the unread message counts for the current user with default parameters
var unreadMessageCounts = chat.GetUnreadMessagesCounts();

// process and display the retrieved unread message counts
foreach (var unreadMessage in unreadMessageCounts)
{
Console.WriteLine($"Channel ID: {unreadMessage.ChannelId}, Unread Messages: {unreadMessage.Count}");
}
}
else
show all 18 lines

Mark messages as read (one channel)

SetLastReadMessage() and SetLastReadMessageTimeToken() let you set the timetoken of the last read message on a given channel to set a timeline from which you can count unread messages. You choose on your own which user action it is bound to.

Setting the last read message for users lets you implement the Read Receipts feature and monitor which channel member read which message.

Method signature

These methods take the following parameters:

  • SetLastReadMessage()

    membership.SetLastReadMessage(Message message)
  • SetLastReadMessageTimeToken()

    membership.SetLastReadMessageTimeToken(string timeToken)

Input

ParameterTypeRequired by SetLastReadMessage()Required by SetLastReadMessageTimeToken()DefaultDescription
messageMessageYesNon/aLast read message on a given channel with the timestamp that gets added as the lastReadMessageTimetoken property to the C++ parent object from which the Membership object inherits.
timeTokenstringNoYesn/aTimetoken of the last read message on a given channel that gets added as the lastReadMessageTimetoken property to the C++ parent object from which the Membership object inherits.

Output

These methods don't return any data.

Basic usage

Set the message with the 16200000000000001 timetoken as the last read message for the support_agent_15 user on the support channel.

  • SetLastReadMessage()

    // reference the "support_agent_15" user
    if (chat.TryGetUser("support_agent_15", out var user))
    {
    Console.WriteLine($"Found user with name {user.Name}");

    // get the list of all user memberships
    var memberships = user.GetMemberships();

    // filter out the right channel
    var membership = memberships.FirstOrDefault(m => m.ChannelId == "support");
    if (membership != null)
    {
    Console.WriteLine($"Found membership for channel: {membership.Channel.Name}");

    // reference the "support" channel
    show all 47 lines
  • SetLastReadMessageTimeToken()

    // reference the "support_agent_15" user
    if (chat.TryGetUser("support_agent_15", out var user))
    {
    Console.WriteLine($"Found user with name {user.Name}");

    // get the list of all user memberships
    var memberships = user.GetMemberships();

    // filter out the right channel
    var membership = memberships.FirstOrDefault(m => m.ChannelId == "support");
    if (membership != null)
    {
    Console.WriteLine($"Found membership for channel: {membership.ChannelId}");

    // reference the "support" channel
    show all 38 lines

Mark messages as read (all channels)

MarkAllMessagesAsRead() lets you mark as read all messages you didn't read on all joined channels.

Method signature

This method has the following signature:

chat.MarkAllMessagesAsRead(
string filter = "",
string sort = "",
int limit = 0,
Page page = null
)

Input

ParameterTypeRequiredDefaultDescription
filterstringNoempty stringExpression used to filter the results. Returns only these unread messages whose properties satisfy the given expression. The filter language is defined here.
sortstringNoempty stringKey-value pair of a property to sort by, and a sort direction. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. By default, the items are sorted by the last updated date.
limitnumberNo0Number of objects to return in response.
pagePageNonullObject used for pagination to define which previous or next result page you want to fetch.

Output

TypeDescription
MarkMessagesAsReadWrapperAn object containing the filtered, sorted, and paginated list of user memberships.

Basic usage

Mark the total number of 50 messages as read and specify you want to fetch the results from the next page using a string that was previously returned from the PubNub server.

// simulating a previously retrieved page token from the PubNub server
string previouslyReturnedNextPageToken = "NPT";

// create an instance of the Page class with the previously returned next page token
Page nextPage = new Page
{
Next = previouslyReturnedNextPageToken
};

// mark a total of 50 messages as read from the next page
var result = chat.MarkAllMessagesAsRead(limit: 50, page: nextPage);

// process the result as needed
Console.WriteLine("Messages marked as read successfully");
Last updated on