Pinned messages
Using Unity Chat SDK methods, you can implement UI features allowing users to "pin" a message to a channel by selecting a sent message and placing it on top of the channel for everyone to see and access it easily.
They can also "unpin" the message if it's no longer important or "pin" another one to replace an already pinned message (there can be only one pinned message on a channel at a time).
Additionally, they can check which message is pinned to the selected channel since the Unity Chat SDK uses the channel metadata to store information on such messages in Message Persistence.
Pinned Messages let users highlight messages in channels for:
-
Essential information - pin critical updates or announcements, ensuring important information remains easily accessible to all participants and reducing the chance of missing key details.
-
Action items and reminders - by pinning action items or reminders, users and their teams can have a centralized location for reference, making it easier to stay organized, prioritize tasks, and ensure accountability.
-
Polling - pin a poll to a channel, allowing a pollster to persist their questions and not have them lost in a chat flow.
Requires App Context and Message Persistence
To store modified data about messages, you must enable App Context and Message Persistence for your app's keyset in the Admin Portal.
Pin
Pin()
and PinMessage()
attach a message to the channel.
Both of these methods give the same result. The only difference between them is that you may or may not be required to provide input parameters, and you call them on different objects: message (Pin()
) or channel (PinMessage()
).
Alternatively, you can also use other Unity Chat SDK methods to pin a message in a thread (thread message) to the thread channel or the parent channel.
Method signature
These methods take the following parameters:
-
Pin()
message.Pin()
-
PinMessage()
channel.PinMessage(Message message)
Input
Parameter | Type | Required in Pin() | Required in PinMessage() | Default | Description |
---|---|---|---|---|---|
message | Message | No | Yes | n/a | Message object that you want to pin to the selected channel. |
Output
These methods don't return any data.
Basic usage
Pin the last message on the incident-management
channel.
-
Pin()
show all 26 lines// get the "incident-management" channel
if (chat.TryGetChannel("incident-management", out var channel))
{
Console.WriteLine($"Found channel with name {channel.Name}");
// retrieve the message history with the desired count
var messageHistory = channel.GetMessageHistory(null, null, 1);
// get the last message from the returned list
var lastMessage = messageHistory.Messages.FirstOrDefault();
// pin the last message if it exists
if (lastMessage != null)
{
lastMessage.Pin(); -
PinMessage()
show all 26 lines// get the "incident-management" channel
if (chat.TryGetChannel("incident-management", out var channel))
{
Console.WriteLine($"Found channel with name {channel.Name}");
// retrieve the message history with the desired count
var messageHistory = channel.GetMessageHistory(null, null, 1);
// get the last message from the returned list
var lastMessage = messageHistory.Messages.FirstOrDefault();
// pin the last message using the PinMessage method if it exists
if (lastMessage != null)
{
channel.PinMessage(lastMessage);
Get
TryGetPinnedMessage()
fetches the message that is currently pinned to the channel.
Method signature
This method has the following signature:
channel.TryGetPinnedMessage(out Message pinnedMessage)
Input
This method doesn't take any parameters.
Output
Parameter | Type | Description |
---|---|---|
pinnedMessage | out Message | The parameter to hold the pinned message if successfully retrieved; otherwise, null . |
n/a | bool | Indicates whether the pinned message was successfully retrieved (true ) or not (false ). |
Basic usage
Get the message pinned to the incident-management
channel.
if (chat.TryGetChannel("incident-management", out var channel))
{
Console.WriteLine($"Found channel with name {channel.Name}");
// Try to get the pinned message from the channel
if (channel.TryGetPinnedMessage(out Message pinnedMessage))
{
Console.WriteLine("Pinned message found: " + pinnedMessage.MessageText);
}
else
{
Console.WriteLine("No pinned message found.");
}
}
else
show all 18 linesUnpin
UnpinMessage()
unpins a message from the channel.
Alternatively, you can also use other Unity Chat SDK methods to unpin a message in a thread (thread message) from the thread channel or the parent channel.
Method signature
This method has the following signature:
channel.UnpinMessage()
Input
This method doesn't take any parameters.
Output
This method doesn't return any data.
Basic usage
Unpin the message from the incident-management
channel.
// attempt to get the channel named "incident-management"
if (chat.TryGetChannel("incident-management", out var channel))
{
Console.WriteLine($"Found channel with name {channel.Name}");
// attempt to unpin a message
try
{
channel.UnpinMessage();
Console.WriteLine("Message has been unpinned successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to unpin the message: {ex.Message}");
}
show all 20 lines