Pinned messages

Pin messages to channels for easy access. Only one message can be pinned per channel at a time.

Use cases:

  • Essential announcements and updates
  • Action items and reminders
  • Polls and persistent questions
Requires App Context and Message Persistence

Pin​

pin() and pinMessage() attach a message to a channel. Call pin() on a message object or pinMessage() on a channel object.

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() (on the Message object)

    1message.pin() async throws -> ChannelImpl
  • pinMessage() (on the Channel object)

    1channel.pinMessage(
    2 message: MessageImpl
    3) async throws -> ChannelImpl

Input​

message#
Type: MessageImpl
Default: no default
MessageImpl object that you want to pin to the selected channel.
Required in pin()
No
Required in pinMessage()
Yes

Output​

ChannelImpl#
The updated channel metadata.

Sample code​

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Pin the last message on the incident-management channel.

  • pin()

    1// Assumes a "ChatImpl" reference named "chat"
    2Task {
    3 if let channel = try await chat.getChannel(channelId: "incident-management") {
    4 if let message = try await channel.getHistory(count: 1).messages.first {
    5 let pinnedChannel = try await message.pin()
    6 debugPrint("A message was pinned to the channel: \(pinnedChannel)")
    7 } else {
    8 debugPrint("The channel history is empty. No message to pin.")
    9 }
    10 } else {
    11 debugPrint("Channel not found")
    12 }
    13}
  • pinMessage()

    1// Assumes a "ChatImpl" reference named "chat"
    2Task {
    3 if let channel = try await chat.getChannel(channelId: "incident-management") {
    4 if let message = try await channel.getHistory(count: 1).messages.first {
    5 let pinnedChannel = try await channel.pinMessage(message: message)
    6 debugPrint("A message was pinned to the channel: \(pinnedChannel)")
    7 } else {
    8 debugPrint("The channel history is empty. No message to pin.")
    9 }
    10 } else {
    11 debugPrint("Channel not found")
    12 }
    13}

Get​

getPinnedMessage() retrieves the currently pinned message.

Method signature​

This method has the following signature:

1channel.getPinnedMessage() async throws -> MessageImpl?

Input​

This method doesn't take any parameters.

Output​

MessageImpl#

Sample code​

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Get the message pinned to the incident-management channel.

1// Assumes a "ChatImpl" reference named "chat"
2Task {
3 if let channel = try await chat.getChannel(channelId: "incident-management") {
4 if let message = try await channel.getPinnedMessage() {
5 print("Pinned message content: \(message.content)")
6 } else {
7 debugPrint("No pinned message found in the channel.")
8 }
9 } else {
10 debugPrint("Channel not found")
11 }
12}

Unpin​

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:

1channel.unpinMessage() async throws -> ChannelImpl

Input​

This method doesn't take any parameters.

Output​

ChannelImpl#
The updated channel metadata.

Sample code​

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Unpin the message from the incident-management channel.

1// Assumes a "ChatImpl" reference named "chat"
2Task {
3 if let channel = try await chat.getChannel(channelId: "incident-management") {
4 let updatedChannel = try await channel.unpinMessage()
5 debugPrint("Updated channel object: \(updatedChannel)")
6 } else {
7 debugPrint("Channel not found")
8 }
9}

Was this page useful?

Last updated on