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
| Parameter | Description | 
|---|---|
| timetoken*Type:  LongDefault: n/a | Timetoken of the message you want to retrieve from Message Persistence. | 
Output
| Type | Description | 
|---|---|
| PNFuture<Message?> | Returned Messageobject. | 
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}")
Get historical details
If you have Message Persistence enabled on your keyset, PubNub stores all historical info about messages, their metadata, and reactions.
If you want to fetch historical details of a given message, use the getHistory() method. By default, when you fetch historical messages, PubNub returns all message actions and metadata attached to the retrieved messages.
Get message content
You can access the text property of the Message object to receive its text content.
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
You can access the deleted property of the Message object to check if it was deleted.
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}