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.

Unreal Chat SDK stores info about the timetoken of the last message the current user read on a given channel on the lastReadMessageTimetoken property. lastReadMessageTimetoken is available on the custom parameter 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.

icon

Usage in Blueprints and C++


Requires App Context and Message Persistence

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

Get last read message

You can use a getter method to check what is the timetoken of the last message a user read on a given channel. LastReadMessageTimetoken() 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

Output

TypeDescription
FStringValue of the returned timetoken.

Basic usage

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

#include "Kismet/GameplayStatics.h"
#include "PubnubChatSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

// Define user ID
FString UserID = "support_agent_15";

// Get the user and save the reference
UPubnubUser* User = Chat->GetUser(UserID);
FString Filter = "channel.id == 'support'";

show all 20 lines

Get unread messages count (one channel)

GetUnreadMessageCount() 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:

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.

#include "Kismet/GameplayStatics.h"
#include "PubnubChatSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

// Define user ID
FString UserID = "support_agent_15";

// Get the user and save the reference
UPubnubUser* User = Chat->GetUser(UserID);
FString Filter = "channel.id == 'support'";

show all 20 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

Output

ParameterTypeDescription
TArray<FPubnubUnreadMessageWrapper>TArrayArray of FPubnubUnreadMessageWrapper structs containing these fields: Channel, Membership, and Count. The method returns an array of such objects corresponding to all channel memberships.
 → ChannelUPubnubChannel*Channel with unread messages.
 → MembershipUPubnubMembership*Returned Membership object showing the user-channel data.
 → countintTotal number of messages unread by the current user on a given channel.

Basic usage

Get the number of all messages unread by the support_agent_15 user on all joined channels.

#include "Kismet/GameplayStatics.h"
#include "PubnubChatSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

TArray<FPubnubUnreadMessageWrapper> UnreadMessagesOnAllChannels = Chat->GetUnreadMessagesCounts();

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

Output

TypeDescription
UPubnubMembership*Returned Membership object updated with the lastReadMessageTimetoken custom parameter.

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()

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    // Define user ID
    FString UserID = "support_agent_15";

    // Get the user and save the reference
    UPubnubUser* User = Chat->GetUser(UserID);
    FString Filter = "channel.id == 'support'";

    show all 28 lines
  • SetLastReadMessageTimetoken()

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    // Define user ID
    FString UserID = "support_agent_15";

    // Get the user and save the reference
    UPubnubUser* User = Chat->GetUser(UserID);
    FString Filter = "channel.id == 'support'";

    show all 25 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:

Output

ParameterTypeDescription
FPubnubMarkMessagesAsReadWrapperstructReturned object containing these fields: page, total, status, and memberships.
 → PageFPubnubPageObject used for pagination to define which previous or next result page you want to fetch.
   → NextFStringRandom string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
   → PrevFStringRandom string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied.
 → TotalintTotal number of messages marked as read.
 → StatusintStatus code of a server response, like 200.
 → MembershipsTArray<UPubnubMembership*>Array of all related 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.

#include "Kismet/GameplayStatics.h"
#include "PubnubChatSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

// Define the limit for the number of messages to mark as read
int Limit = 50;

// Example pagination token previously returned from the server
FString NextPageToken = "your_next_page_token_here";

// Define the page object for forward pagination
show all 20 lines
Last updated on