Unread messages
Track unread message counts for users who reconnect after being offline.
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
Enable App Context and Message Persistence in the Admin Portal.
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
- Blueprint
- C++ / Input parameters
1Membership->LastReadMessageTimetoken();
Output
| Type | Description |
|---|---|
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 linesGet 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:
- Blueprint
- C++ / Input parameters
1Membership->GetUnreadMessageCount();
Output
| Type | Description |
|---|---|
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 linesGet 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
- Blueprint
- C++ / Input parameters
1Chat->GetUnreadMessagesCounts(
2 int Limit,
3 FString Start,
4 FString End,
5 FString Filter = "")
6;
| Parameter | Description |
|---|---|
FilterType: FString (Memberships filter)Default: n/a | Filter expression evaluated against channel memberships only. Allowed targets: channel.* plus common fields (status, type, custom). uuid.* fields aren't supported. |
SortType: FStringDefault: n/a | Key-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. |
LimitType: intDefault: 100 | Number of objects to return in response. The default (and maximum) value is 100. |
PageType: FPubnubPageDefault: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
→ NextType: FStringDefault: n/a | 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. |
→ PrevType: FStringDefault: n/a | 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. |
Output
| Parameter | Description |
|---|---|
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. |
→ ChannelType: UPubnubChannel* | Channel with unread messages. |
→ MembershipType: UPubnubMembership* | Returned Membership object showing the user-channel data. |
→ countType: 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
- Blueprint
- C++ / Input parameters
-
SetLastReadMessage()1Membership->SetLastReadMessage(UPubnubMessage* Message); -
SetLastReadMessageTimetoken()1Membership->SetLastReadMessageTimetoken(FString Timetoken);
| Parameter | Required by SetLastReadMessage() | Required by SetLastReadMessageTimetoken() | Description |
|---|---|---|---|
MessageType: UPubnubMessage*Default: n/a | Yes | No | Last read message on a given channel with the timestamp that gets added to the user-channel membership as the custom parameter called lastReadMessageTimetoken. |
TimetokenType: FStringDefault: n/a | No | Yes | Timetoken of the last read message on a given channel that gets added to the user-channel membership as the custom parameter called lastReadMessageTimetoken. |
Output
| Type | Description |
|---|---|
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()
show all 28 lines1#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 -
SetLastReadMessageTimetoken()
show all 25 lines1#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
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:
- Blueprint
- C++ / Input parameters
1Chat->MarkAllMessagesAsRead(
2 FString Filter = "",
3 FString Sort = "",
4 int Limit = 0,
5 FPubnubPage Page = FPubnubPage()
6);
| Parameter | Description |
|---|---|
FilterType: FString (Memberships filter)Default: n/a | Filter expression evaluated against channel memberships only. Allowed targets: channel.* plus common fields (status, type, custom). uuid.* fields aren't supported. |
SortType: FStringDefault: n/a | Key-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. |
LimitType: intDefault: 100 | Number of objects to return in response. The default (and maximum) value is 100. |
PageType: FPubnubPageDefault: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
→ NextType: FStringDefault: n/a | 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. |
→ PrevType: FStringDefault: n/a | 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. |
Output
| Parameter | Description |
|---|---|
FPubnubMarkMessagesAsReadWrapperType: struct | Returned object containing these fields: page, total, status, and memberships. |
→ PageType: FPubnubPage | Object used for pagination to define which previous or next result page you want to fetch. |
→ NextType: 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. |
→ PrevType: 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. |
→ TotalType: int | Total number of messages marked as read. |
→ StatusType: int | Status code of a server response, like 200. |
→ MembershipsType: 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