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.

// Create pinned message
val pinExpirationDate = Calendar.getInstance().apply { set(2077, 1, 1) }.time
val message = Message(
  text = "Hey punk",
  pinned = true,
  pinExpires = pinExpirationDate
)

channel.sendMessage(message).enqueue { /* ... */ }

// Unpin message
channel.unpinMessage(message).enqueue { /* ... */ }

// Pin message for 120 seconds
channel.pinMessage(message, timeout = 120).enqueue { /* ... */ }

// Change message expiration to 2077
channel.pinMessage(message, expirationDate = pinExpirationDate).enqueue { /* ... */ }

// Remove expiration date from pinned message
channel.pinMessage(message, expirationDate = null).enqueue { /* ... */ }
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:

channelClient.query(QueryChannelRequest()).enqueue { result ->
  if (result.isSuccess) {
    val pinnedMessages: List<Message> = result.data().pinnedMessages
  } else {
    // Handle result.error()
  }
}

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:

// List the first page of pinned messages, pinned before now, of the channel with descending direction (newest on top)
channelClient.getPinnedMessages(
  limit = 10,
  sort = QuerySortByField.descByName("pinnedAt"),
  pagination = PinnedMessagesPagination.BeforeDate(
    date = Date(),
    inclusive = false,
  ),
).enqueue { result ->
  if (result.isSuccess) {
    val pinnedMessages: List<Message> = result.data()
  } else {
    // Handle result.error()
  }
}

// You can use a pinnedAt date retrieved from the previous request to get the next page
val nextDate = Date()
// List the next page of pinned messages
channelClient.getPinnedMessages(
  limit = 10,
  sort = QuerySortByField.descByName("pinnedAt"),
  pagination = PinnedMessagesPagination.BeforeDate(
    date = nextDate,
    inclusive = false,
  ),
).enqueue { result ->
  if (result.isSuccess) {
    val pinnedMessages: List<Message> = result.data()
  } else {
    // Handle result.error()
  }
}
© Getstream.io, Inc. All Rights Reserved.