Get message details
Get message details, retrieve content, and check if the message was deleted.
Get message details
TryGetMessage() and GetMessageAsync fetch the Message object from Message Persistence based on the message timetoken.
Method signature
- Sync
- Async
channel.TryGetMessage(
string timeToken,
out Message message
)
Input
| Parameter | Description |
|---|---|
timeToken *Type: stringDefault: n/a | Timetoken of the message you want to retrieve from Message Persistence. |
message *Type: out MessageDefault: n/a | The message to populate with the appropriate Message object if the method returns true. If it returns false, the message remains uninitialized. |
Output
If the method returns true, the out message parameter is populated with the appropriate Message object. If it returns false, the message remains uninitialized.
Sample code
Get the message with the 16200000000000001 timetoken.
// reference the "support" channel
if (!chat.TryGetChannel("support", out var channel))
{
Console.WriteLine("Couldn't find channel!");
return;
};
// get the message
if (channel.TryGetMessage("16200000000000001", out var message))
{
Console.WriteLine($"Message: {message.MessageText}");
}
channel.GetMessageAsync(
string timeToken
)
Input
| Parameter | Description |
|---|---|
timeToken *Type: stringDefault: n/a | Timetoken of the message you want to retrieve from Message Persistence. |
Output
Returns an awaitable Task<Message?> with the fetched Message object or null if there wasn't one with the specified timeToken.
Sample code
Get the message with the 16200000000000001 timetoken.
// reference the "support" channel
if (!chat.TryGetChannel("support", out var channel))
{
Console.WriteLine("Couldn't find channel!");
return;
};
// get the message
var message = await channel.GetMessageAsync("16200000000000001");
if (message != null)
{
Console.WriteLine($"Message: {message.MessageText}");
}
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 GetMessageHistory() method. By default, when you fetch historical messages, PubNub returns all message reactions and metadata attached to the retrieved messages.
Get message content
You can access the MessageText property of the Message object to receive its text content.
Method signature
This is how you can access the property:
message.MessageText: string
Properties
| Property | Description |
|---|---|
MessageTextType: string | Text content of the returned message. |
Sample code
Get the content of the message with the 16200000000000000 timetoken.
// reference the "support" channel
if (!chat.TryGetChannel("support", out var channel))
{
Console.WriteLine("Couldn't find channel!");
return;
};
// get the message
if (channel.TryGetMessage("16200000000000001", out var message))
{
Console.WriteLine($"Message: {message.MessageText}");
}
Check deletion status
You can access the IsDeleted property of the Message object to check if it's deleted.
Method signature
This is how you can access the property:
message.IsDeleted: bool
Properties
| Property | Description |
|---|---|
IsDeletedType: bool | Info on whether the message has the IsDeleted flag set to true or not. |
Sample code
Get the status of the message with the 16200000000000000 timetoken.
// get the message
// reference the "support" channel
if (!chat.TryGetChannel("support", out var channel))
{
Console.WriteLine("Couldn't find channel!");
return;
};
// get the message
if (channel.TryGetMessage("16200000000000000", out var message))
{
Console.WriteLine($"Is deleted?: {message.IsDeleted}");
}