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.
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
- Blueprint
- C++ / Input parameters
Membership->LastReadMessageTimetoken();
Output
Type | Description |
---|---|
FString | Value 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 linesGet 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:
- Blueprint
- C++ / Input parameters
Membership->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. |
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 linesGet 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
- Blueprint
- C++ / Input parameters
Chat->GetUnreadMessagesCounts(
int Limit,
FString Start,
FString End,
FString Filter = "")
;
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
Filter | FString | No | n/a | Expression used to filter the results. Returns only these channels whose properties satisfy the given expression are returned. The filter language is defined here. |
Sort | FString | No | 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. |
Limit | int | No | 100 | Number of objects to return in response. The default (and maximum) value is 100 . |
Page | FPubnubPage | No | n/a | Object used for pagination to define which previous or next result page you want to fetch. |
→ Next | FString | No | 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. |
→ Prev | FString | No | 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 | Type | Description |
---|---|---|
TArray<FPubnubUnreadMessageWrapper> | 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 | UPubnubChannel* | Channel with unread messages. |
→ Membership | UPubnubMembership* | Returned Membership object showing the user-channel data. |
→ count | int | Total 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
- Blueprint
- C++ / Input parameters
-
SetLastReadMessage()
Membership->SetLastReadMessage(UPubnubMessage* Message);
-
SetLastReadMessageTimetoken()
Membership->SetLastReadMessageTimetoken(FString Timetoken);
Parameter | Type | Required by SetLastReadMessage() | Required by SetLastReadMessageTimetoken() | Default | Description |
---|---|---|---|---|---|
Message | UPubnubMessage* | Yes | No | n/a | Last read message on a given channel with the timestamp that gets added to the user-channel membership as the custom parameter called lastReadMessageTimetoken . |
Timetoken | FString | No | Yes | n/a | 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. |
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()
show all 28 lines#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'"; -
SetLastReadMessageTimetoken()
show all 25 lines#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'";
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:
- Blueprint
- C++ / Input parameters
Chat->MarkAllMessagesAsRead(
FString Filter = "",
FString Sort = "",
int Limit = 0,
FPubnubPage Page = FPubnubPage()
);
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
Filter | FString | No | n/a | Expression used to filter the results. Returns only these channels whose properties satisfy the given expression are returned. The filter language is defined here. |
Sort | FString | No | 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. |
Limit | int | No | 100 | Number of objects to return in response. The default (and maximum) value is 100 . |
Page | FPubnubPage | No | n/a | Object used for pagination to define which previous or next result page you want to fetch. |
→ Next | FString | No | 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. |
→ Prev | FString | No | 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 | Type | Description |
---|---|---|
FPubnubMarkMessagesAsReadWrapper | struct | Returned object containing these fields: page , total , status , and memberships . |
→ Page | FPubnubPage | Object used for pagination to define which previous or next result page you want to fetch. |
→ Next | 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 | 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 | int | Total number of messages marked as read. |
→ Status | int | Status code of a server response, like 200 . |
→ Memberships | TArray<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