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.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get the timetoken of the last message read by the support_agent_15 user on the support channel.
1
Get unread messages count (one channel)
getUnreadMessagesCount() 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:
1membership.getUnreadMessagesCount() async throws -> UInt64?
Input
This method doesn't take any parameters.
Output
| Parameter | Description |
|---|---|
UInt64 | Retrieves from Message Persistence the number (count) of all messages unread by a given user on a given channel from the last read message. The method returns null when there is no last read message timetoken set on the MessageImpl object. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get the number of all messages unread by the support_agent_15 user on the support channel.
1
Get unread messages count (all channels)
fetchUnreadMessagesCounts() 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
This method has the following signature:
1chat.fetchUnreadMessagesCounts(
2 limit: Int? = nil,
3 page: PubNubHashedPage? = nil,
4 filter: String? = nil,
5 sort: [PubNub.MembershipSortField] = [],
6 completion: ((Swift.Result<(countsByChannel: [GetUnreadMessagesCount<ChannelImpl, MembershipImpl>], page: PubNubHashedPage?), Error>) -> Void)? = nil
7)
Input
| Parameter | Description |
|---|---|
limitType: IntDefault: nil | Number of objects to return in response. The default (and maximum) value is 100. |
pageType: PubNubHashedPageDefault: nil | Object used for pagination to define which previous or next result page you want to fetch. |
filterType: String (Memberships filter)Default: nil | Filter expression evaluated against channel memberships only. Allowed targets: channel.* plus common fields (status, type, custom). uuid.* fields aren't supported. |
sortType: [PubNub.MembershipSortField]Default: [] | Array of sorting criteria 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 nil to take the default sorting direction (ascending). By default, the items are sorted by the last updated date. |
completionType: ((Swift.Result<(countsByChannel: [GetUnreadMessagesCount<ChannelImpl, MembershipImpl>], page: PubNubHashedPage?), Error>) -> Void)?Default: nil | Completion closure called with the result of the operation. |
Output
| Parameter | Description |
|---|---|
Swift.Result<>Type: object | Result object containing either success data or an error. |
→ countsByChannelType: [GetUnreadMessagesCount<ChannelImpl, MembershipImpl>] | Array of objects containing channel, membership, and count information. |
→ channelType: ChannelImpl | Channel with unread messages. |
→ membershipType: MembershipImpl | Returned Membership object showing the user-channel data. |
→ countType: UInt64 | Total number of messages unread by the current user on a given channel. |
→ pageType: PubNubHashedPage? | Object containing pagination information. |
→ startType: string? | Token for fetching the next page of results. |
→ endType: string? | Token for fetching the previous page of results. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get the number of all messages unread by the support_agent_15 user on all joined channels.
1
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 for users lets you implement the Read Receipts feature and monitor which channel member read which message.
Method signature
These methods take the following parameters:
-
setLastReadMessage()1membership.setLastReadMessage(
2 message: MessageImpl
3) async throws -> MembershipImpl -
setLastReadMessageTimetoken()1membership.setLastReadMessageTimetoken(
2 _ timetoken: Timetoken
3) async throws -> MembershipImpl
Input
| Parameter | Required by setLastReadMessage() | Required by setLastReadMessageTimetoken() | Description |
|---|---|---|---|
messageType: MessageImplDefault: n/a | Yes | No | Last read message on a given channel with the timestamp that gets added to the user-channel membership as the lastReadMessageTimetoken property. |
timetokenType: TimetokenDefault: n/a | No | Yes | Timetoken of the last read message on a given channel that gets added to the user-channel membership as the lastReadMessageTimetoken property. |
Output
| Parameter | Description |
|---|---|
MembershipImpl | Returned MembershipImpl object updated with the lastReadMessageTimetoken parameter. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Set the message with the 16200000000000001 timetoken as the last read message for the support_agent_15 user on the support channel.
-
setLastReadMessage()1 -
setLastReadMessageTimetoken()1
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:
1chat.markAllMessagesAsRead(
2 limit: Int? = nil,
3 page: PubNubHashedPage? = nil,
4 filter: String? = nil,
5 sort: [PubNub.MembershipSortField] = []
6 ) async throws -> (memberships: [MembershipImpl], page: PubNubHashedPage?)
Input
| Parameter | Description |
|---|---|
limitType: IntDefault: 100 | Number of objects to return in response. The default (and maximum) value is 100. |
pageType: PubNubHashedPageDefault: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
filterType: StringDefault: n/a | Expression used to filter the results. Returns only these messages whose properties satisfy the given expression. The filter language is defined here. |
sortType: [PubNub.MembershipSortField]Default: listOf() | 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. |
Output
| Parameter | Description |
|---|---|
(memberships: [MembershipImpl], page: PubNubHashedPage?) | Returned object with a list of memberships and a page object with these fields: next, prev, total, and status. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
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
Get unread messages count (all channels) (deprecated)
Deprecated
Use fetchUnreadMessagesCounts() instead.
getUnreadMessagesCounts() returns unread counts for all joined channels.
Method signature
This method has the following signature:
1chat.getUnreadMessagesCount(
2 limit: Int? = nil,
3 page: PubNubHashedPage? = nil,
4 filter: String? = nil,
5 sort: [PubNub.MembershipSortField] = []
6) async throws -> [GetUnreadMessagesCount<ChannelImpl, MembershipImpl>]
Input
| Parameter | Description |
|---|---|
limitType: IntDefault: 100 | Number of objects to return in response. The default (and maximum) value is 100. |
pageType: PubNubHashedPageDefault: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
filterType: StringDefault: n/a | Expression used to filter which channel memberships to include in the results. Returns unread message counts only for channels whose membership properties satisfy the given expression. The filter language is defined here. |
sortType: [PubNub.MembershipSortField]Default: listOf() | 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. |
Output
| Parameter | Description |
|---|---|
[GetUnreadMessagesCount<ChannelImpl, MembershipImpl>] | An array of GetUnreadMessagesCount representing unread messages for the current user in a given channel. It returns these details: channel, membership, count. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get the number of all messages unread by the support_agent_15 user on all joined channels.
1