Pinned messages

Pinned messages allow users to highlight important messages, make announcements, or temporarily promote content. Pinning a message is, by default, restricted to certain user roles, but this is flexible. Each channel can have multiple pinned messages and these can be created or updated with or without an expiration.

Pin and unpin a message

An existing message can be updated to be pinned or unpinned by using the .pinMessage and .unpinMessage methods. Or a new message can be pinned when it is sent by setting the pinned and pin_expires fields when using channel.sendMessage.

let messageController = client.messageController(cid: .init(type: .messaging, id: "general"), messageId: "message-id")

// Pin message for 120 seconds
messageController.pin(.expirationTime(120))

// Unpin message
messageController.unpin()

// Pin message to specific date
messageController.pin(.expirationDate(Date()))
nametypedescriptiondefaultoptional
pinnedbooleanIndicates whether the message is pinned or notfalse
pinned_atstringDate when the message got pinned-
pin_expiresstringDate when the message pin expires. An empty value means that message does not expirenull
pinned_byUserThe user that pinned the message-

To pin the message user has to have PinMessage permission. You can find the list of permissions and defaults in Permission Resources and Default Permissions sections

Retrieve pinned messages

You can easily retrieve the last 10 pinned messages from the channel.pinned_messages field:

let channelController = client.channelController(for: .init(type: .messaging, id: "general"))

channelController.synchronize() { error in
  if error == nil {
    channelController.channel?.pinnedMessages
  }
}

To learn more about channels you can visit Querying Channels page

Paginate over all pinned messages

Stream Chat also provides dedicated endpoint for pinned messages of the channel which can be used to retrieve all pinned messages page by page:

channelController.loadPinnedMessages(
  pageSize: 10,
  sorting: [.init(key: .pinnedAt, isAscending: false)],
  pagination: .earlier(Date(), inclusive: true)) { result in
    switch result {
    case .success(let pinnedMessages):
      print(pinnedMessages)
    case .failure(let failure):
      // …
    }
  }
// Next page can be fetched using before message id
let fetchedPinnedMessageId = …
channelController.loadPinnedMessages(
  pagination: .before(fetchedPinnedMessageId, inclusive: false)) { result in
    // …
  }
© Getstream.io, Inc. All Rights Reserved.