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.

// Android SDK

// Create pinned message
Calendar calendar = Calendar.getInstance();
calendar.set(2077, 1, 1);
Date pinExpirationDate = calendar.getTime();

Message message = new Message();
message.setText("Hey punk");
message.setPinned(true);
message.setPinExpires(pinExpirationDate);

channelClient.sendMessage(message).enqueue(result -> { /* ... */ });

// Unpin message
channelClient.unpinMessage(message).enqueue(result -> { /* ... */ });

// Pin message for 120 seconds
channelClient.pinMessage(message, 120).enqueue(result -> { /* ... */ });

// Change message expiration to 2077
channelClient.pinMessage(message, pinExpirationDate).enqueue(result -> { /* ... */ });

// Remove expiration date from pinned message
channelClient.pinMessage(message, null).enqueue(result -> { /* ... */ });

// Backend SDK

// create pinned message
Message message =
  Message.send(channelType, channelId)
    .message(
      MessageRequestObject.builder()
        .text("my message")
        .pinned(true)
        .pinExpires(
          new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
            .parse("2077-01-01T00:00:00Z"))
        .userId(userId)
        .build())
    .request()
    .getMessage();

// unpin message
MessageRequestObject messageRequestObject = MessageRequestObject.buildFrom(message);
messageRequestObject.setPinned(false);
Message message2 =
  Message.update(message.getId()).message(messageRequestObject).request().getMessage();

// pin message for 120 seconds
MessageRequestObject messageRequestObject2 = MessageRequestObject.buildFrom(message2);
messageRequestObject2.setPinned(true);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 120);
messageRequestObject2.setPinExpires(calendar.getTime());
Message message3 =
  Message.update(message2.getId()).message(messageRequestObject2).request().getMessage();

// change message expiration to 2077
MessageRequestObject messageRequestObject3 = MessageRequestObject.buildFrom(message3);
messageRequestObject3.setPinExpires(
  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse("2077-01-01T00:00:00Z"));
Message message4 =
  Message.update(message3.getId()).message(messageRequestObject3).request().getMessage();

// remove expiration date from pinned message
MessageRequestObject messageRequestObject4 = MessageRequestObject.buildFrom(message4);
messageRequestObject4.setPinExpires(null);
Message.update(message4.getId()).message(messageRequestObject4).request();
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:

// Android SDK
channelClient.query(new QueryChannelRequest()).enqueue(result -> {
  if (result.isSuccess()) {
    List<Message> pinnedMessages = result.data().getPinnedMessages();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
ChannelGetResponse resp = Channel.getOrCreate("type", "id").request();
List<Message> messages = resp.getPinnedMessages();

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:

// Android SDK

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

// You can use a pinnedAt date retrieved from the previous request to get the next page
Date nextDate = new Date();
// List the next page of pinned messages
channelClient.getPinnedMessages(10, QuerySortByField.descByName("pinnedAt"), new PinnedMessagesPagination.BeforeDate(nextDate, false))
    .enqueue(result -> {
      if (result.isSuccess()) {
        List<Message> pinnedMessages = result.data();
      } else {
        // Handle result.error()
      }
    });
    

// Backend SDK
Message.search()
  .filterCondition("pinned", true) // Additional filters can also be applied
  .request();
© Getstream.io, Inc. All Rights Reserved.