Pinned messages
Using 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 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 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(): PNFuture<Channel>
-
pinMessage()
channel.pinMessage(message: Message): PNFuture<Channel>
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
Type | Description |
---|---|
PNFuture<Channel> | PNFuture containing the updated channel metadata. |
Basic usage
Pin the last message on the incident-management
channel.
pin()
fun pinLastMessageInIncidentManagementChannel(chat: Chat) {
// obtain the incident-management channel details
chat.getChannel("incident-management").async { channelResult ->
channelResult.onSuccess { channel ->
// handle success
// retrieve the history of messages
channel.getHistory(count = 1).async { historyResult ->
historyResult.onSuccess { historyResponse ->
// handle success
val messages = historyResponse.messages
if (messages.isNotEmpty()) {
val lastMessage = messages.last()
// pin the last message
lastMessage.pin().async { pinResult ->
show all 37 linespinMessage()
fun pinLastMessageInIncidentManagementChannel(chat: Chat) {
// obtain the incident-management channel details
chat.getChannel("incident-management").async { channelResult ->
channelResult.onSuccess { channel ->
// handle success
// retrieve the history of messages
channel.getHistory(count = 1).async { historyResult ->
historyResult.onSuccess { historyResponse ->
// handle success
val messages = historyResponse.messages
if (messages.isNotEmpty()) {
// get the last message
val lastMessage = messages.last()
// pin the last message
show all 38 linesGet
getPinnedMessage()
fetches the message that is currently pinned to the channel.
Method signature
This method has the following signature:
channel.getPinnedMessage(): PNFuture<Message?>
Input
This method doesn't take any parameters.
Output
Type | Description |
---|---|
PNFuture<Message?> | Returned Message object. |
Basic usage
Get the message pinned to the incident-management
channel.
chat.getChannel("incident-management").async { result ->
result.onSuccess { channel ->
val pinnedMessageFuture = channel.getPinnedMessage()
pinnedMessageFuture.async { pinnedMessageResult ->
pinnedMessageResult.onSuccess { message ->
if (message != null) {
// handle success
println("Pinned message content: ${message.content}")
} else {
println("No pinned message found in the channel.")
}
}.onFailure { exception ->
// handle failure
exception.printStackTrace()
show all 24 linesUnpin
unpinMessage()
unpins a message from the channel.
Alternatively, you can also use other 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(): PNFuture<Channel>
Input
This method doesn't take any parameters.
Output
Type | Description |
---|---|
PNFuture<Channel> | PNFuture containing the updated channel metadata. |
Basic usage
Unpin the message from the incident-management
channel.
chat.getChannel("incident-management").async { result ->
result.onSuccess { channel ->
// attempt to unpin the message
val unpinMessageFuture = channel.unpinMessage()
unpinMessageFuture.async { unpinMessageResult ->
unpinMessageResult.onSuccess { updatedChannel ->
// handle success
println("Message successfully unpinned from the channel.")
}.onFailure { exception ->
// handle failure
exception.printStackTrace()
println("Failed to unpin the message: ${exception.message}")
}
}
show all 21 lines