On this page

Unread messages

Track unread message counts for users who reconnect after being offline.

icon

Usage in Blueprints and C++


lastReadMessageTimetoken on the Membership object stores when a user last read messages on a channel. This is set automatically on Join() or Invite(). Update it based on user actions (scrolling, app focus, etc.) using mark as read methods.

Requires App Context and Message Persistence

Get last read message

LastReadMessageTimetoken() returns the timetoken marking the user's last read position on a channel. This timetoken doesn't always correspond to an actual message. Use it to display unread markers in your UI.

Method signature

Output

TypeDescription
FString
Value of the returned timetoken.

Sample code

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

1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Define user ID
10FString UserID = "support_agent_15";
11
12// Get the user and save the reference
13UPubnubUser* User = Chat->GetUser(UserID);
14FString Filter = "channel.id == 'support'";
15
show all 20 lines

Get unread messages count (one channel)

GetUnreadMessageCount() returns the number of unread messages on a channel. This counts all messages after the last read timetoken, including your own messages.

Method signature

This method has the following signature:

Output

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

Sample code

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

1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Define user ID
10FString UserID = "support_agent_15";
11
12// Get the user and save the reference
13UPubnubUser* User = Chat->GetUser(UserID);
14FString Filter = "channel.id == 'support'";
15
show all 20 lines

Get unread messages count (all channels)

GetUnreadMessagesCounts() returns unread counts for all joined channels with unread messages (channels with zero unread are excluded). Counts include all messages after the last read timetoken, including your own.

Unread counts and filtering

Filters support channel.* fields plus status, type, custom (not uuid.*). Unread counts include your own messages. See Memberships filters.

Method signature

Output

ParameterDescription
TArray<FPubnubUnreadMessageWrapper>
Type: TArray
Array of FPubnubUnreadMessageWrapper structs containing these fields: Channel, Membership, and Count. The method returns an array of such objects corresponding to all channel memberships.
 → Channel
Type: UPubnubChannel*
Channel with unread messages.
 → Membership
Type: UPubnubMembership*
Returned Membership object showing the user-channel data.
 → count
Type: int
Total number of messages unread by the current user on a given channel.

Sample code

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

1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9TArray<FPubnubUnreadMessageWrapper> UnreadMessagesOnAllChannels = Chat->GetUnreadMessagesCounts();

To avoid counting your own recently sent messages as unread, ensure your app updates the last read timetoken.

Mark messages as read (one channel)

SetLastReadMessage() and SetLastReadMessageTimetoken() set the last read timetoken for counting unread messages. Bind these to user actions in your app.

Setting the last read message enables Read Receipts to track which member read which message.

Method signature

Output

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

Sample code

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

  • SetLastReadMessage()

    1#include "Kismet/GameplayStatics.h"
    2#include "PubnubChatSubsystem.h"
    3
    4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
    6
    7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
    8
    9// Define user ID
    10FString UserID = "support_agent_15";
    11
    12// Get the user and save the reference
    13UPubnubUser* User = Chat->GetUser(UserID);
    14FString Filter = "channel.id == 'support'";
    15
    show all 28 lines
  • SetLastReadMessageTimetoken()

    1#include "Kismet/GameplayStatics.h"
    2#include "PubnubChatSubsystem.h"
    3
    4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
    6
    7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
    8
    9// Define user ID
    10FString UserID = "support_agent_15";
    11
    12// Get the user and save the reference
    13UPubnubUser* User = Chat->GetUser(UserID);
    14FString Filter = "channel.id == 'support'";
    15
    show all 25 lines

Mark messages as read (all channels)

MarkAllMessagesAsRead() marks all unread messages as read on all joined channels.

Method signature

This method has the following signature:

Output

ParameterDescription
FPubnubMarkMessagesAsReadWrapper
Type: struct
Returned object containing these fields: page, total, status, and memberships.
 → Page
Type: FPubnubPage
Object used for pagination to define which previous or next result page you want to fetch.
   → Next
Type: FString
Random 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.
   → Prev
Type: FString
Random 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.
 → Total
Type: int
Total number of messages marked as read.
 → Status
Type: int
Status code of a server response, like 200.
 → Memberships
Type: TArray<UPubnubMembership*>
Array of all related memberships.

Sample code

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.

1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Define the limit for the number of messages to mark as read
10int Limit = 50;
11
12// Example pagination token previously returned from the server
13FString NextPageToken = "your_next_page_token_here";
14
15// Define the page object for forward pagination
show all 20 lines
Last updated on