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. |
Sample code
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.
This includes all messages published after your last read timetoken, including messages sent by yourself and all other channel members. The method cannot filter messages by sender - it counts all unread messages regardless of who published them. 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. |
Sample code
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 joined channels that have unread messages.
This method only returns channels with unread message counts greater than 0 - channels with zero unread messages are filtered out. For each returned channel, this includes all messages published after your last read timetoken, including messages sent by yourself and all other channel members. The method cannot filter messages by sender - it counts all unread messages regardless of who published them. 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 | Description |
---|---|
Filter Type: FString Default: 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 Type: FString Default: 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 Type: int Default: 100 | Number of objects to return in response. The default (and maximum) value is 100 . |
Page Type: FPubnubPage Default: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
→ Next Type: FString Default: 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 Type: FString Default: 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. |