On this page

Get message details

Get message details, retrieve content, and check if the message was deleted.

Get message details

getMessage() fetches the Message object from Message Persistence based on the message timetoken.

Method signature

This method takes the following parameters:

1channel.getMessage(timetoken: Long): PNFuture<Message?>

Input

* required
ParameterDescription
timetoken *
Type: Long
Default:
n/a
Timetoken of the message you want to retrieve from Message Persistence.

Output

TypeDescription
PNFuture<Message?>
Returned Message object.

Sample code

Get the message with the 16200000000000001 timetoken.

1chat.getChannel("incident-management").async { channelResult ->
2 channelResult.onSuccess { channel ->
3 // successfully retrieved the channel
4 channel?.getMessage(16200000000000001L)?.async { messageResult ->
5 messageResult.onSuccess { message ->
6 // handle success
7 println("Message retrieved successfully: $message")
8 }.onFailure { error ->
9 // handle failure
10 println("Failed to retrieve the message: ${error.message}")
11 }
12 }
13 }.onFailure { error ->
14 // failed to retrieve the channel
15 println("Failed to retrieve the channel: ${error.message}")
show all 17 lines

Get historical details

With Message Persistence enabled, PubNub stores all historical message data, metadata, and actions.

Fetch historical message details with getHistory(). By default, all message actions and metadata are included in the response.

Get message content

Access the text content of a Message object using the text property.

Sample code

Get the content of the message with the 16200000000000000 timetoken.

1channel.getMessage(16200000000000000).async { result ->
2 result.onSuccess { message: Message? ->
3 println(message?.text ?: "Message not found")
4 }.onFailure { exception: PubNubException ->
5 println("Exception occurred: ${exception.message} ")
6 }
7}

Check deletion status

Check if a Message object was soft-deleted using the deleted property.

Sample code

Get the status of the message with the 16200000000000000 timetoken.

1channel.getMessage(16200000000000000).async { result ->
2 result.onSuccess { message: Message? ->
3 when(message?.deleted){
4 true -> println("Message is deleted")
5 false -> println("Message is not deleted")
6 null -> println("Message does not exist")
7 }
8 }.onFailure { exception: PubNubException ->
9 println("Exception occurred: ${exception.message} ")
10 }
11}
Last updated on