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:
channel.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 Message object. |
Sample code
Get the message with the 16200000000000001 timetoken.
chat.getChannel("incident-management").async { channelResult ->
channelResult.onSuccess { channel ->
// successfully retrieved the channel
channel?.getMessage(16200000000000001L)?.async { messageResult ->
messageResult.onSuccess { message ->
// handle success
println("Message retrieved successfully: $message")
}.onFailure { error ->
// handle failure
println("Failed to retrieve the message: ${error.message}")
}
}
}.onFailure { error ->
// failed to retrieve the channel
println("Failed to retrieve the channel: ${error.message}")
show all 17 linesGet 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.
channel.getMessage(16200000000000000).async { result ->
result.onSuccess { message: Message? ->
println(message?.text ?: "Message not found")
}.onFailure { exception: PubNubException ->
println("Exception occurred: ${exception.message} ")
}
}
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.
channel.getMessage(16200000000000000).async { result ->
result.onSuccess { message: Message? ->
when(message?.deleted){
true -> println("Message is deleted")
false -> println("Message is not deleted")
null -> println("Message does not exist")
}
}.onFailure { exception: PubNubException ->
println("Exception occurred: ${exception.message} ")
}
}