Muting or Hiding Channels

Muting Channels

Messages added to a channel will not trigger push notifications, unhide a channel, nor change the unread count for the users that muted it. By default, mutes stay in place indefinitely until the user removes it; however, you can optionally set an expiration time. The list of muted channels and their expiration time is returned when the user connects.

// Android SDK

// Mute a channel
ChannelClient channelClient = client.channel("messaging", "general");
channelClient.mute().enqueue(result -> {
  if (result.isSuccess()) {
    // Channel is muted
  } else {
    // Handle result.error()
  }
});

// Get list of muted channels when user is connected
User user = new User();
user.setId("user-id");
client.connectUser(user, "token").enqueue(result -> {
  if (result.isSuccess()) {
    // Result contains the list of channel mutes
    List<ChannelMute> mutes = result.data().getUser().getChannelMutes();
  }
 });

// Get updates about muted channels
client.subscribeFor(
    new Class[]{NotificationChannelMutesUpdatedEvent.class},
    channelsMuteEvent -> {
      List<ChannelMute> mutes = ((NotificationChannelMutesUpdatedEvent) channelsMuteEvent).getMe().getChannelMutes();
    }
);

// Backend SDK
Channel.mute()
  .channelCid(channel.getType() + ":" + channel.getId())
  .user(user)
  .expiration(TimeUnit.MILLISECONDS.convert(14, TimeUnit.DAYS)) // Optionally set mute duration
  .request();

Messages added to muted channels do not increase the unread messages count.

Query Muted Channels

Muted channels can be filtered or excluded by using the muted in your query channels filter.

// Android SDK

// Filter for all channels excluding muted ones
FilterObject notMutedFilter = Filters.and(
    Filters.eq("muted", false),
    Filters.in("members", Arrays.asList(currentUserId))
);

// Filter for muted channels
FilterObject mutedFilter = Filters.eq("muted", true);

// Executing a channels query with either of the filters
int offset = 0;
int limit = 10;
QuerySorter<Channel> sort = new QuerySortByField<>();
int messageLimit = 0;
int memberLimit = 0;
client.queryChannels(new QueryChannelsRequest(
    filter, // Set the correct filter here
    offset,
    limit,
    sort,
    messageLimit,
    memberLimit)).enqueue(result -> {
  if (result.isSuccess()) {
    List<Channel> channels = result.data();
  } else {
    // Handle result.error()
  }
});

// Backend SDK

// retrieve all channels excluding muted ones
Channel.list()
  .filterCondition("muted", false) // Or 'true' for selected muted channels
  .filterCondition(FilterCondition.in("members", userId))
  .request();

Remove a Channel Mute

Whenever you need to unmute, you are able to.

// Android SDK

// Unmute channel for current user
channelClient.unmute().enqueue(result -> {
  if (result.isSuccess()) {
    // Channel is unmuted
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Channel.unmute().channelCid(cid).userId(userId).request();

Hiding a Channel

Hiding a channel will remove it from query channel requests for that user until a new message is added. Please keep in mind that hiding a channel is only available to members of that channel. Hidden channels may still have unread messages and you may wish to mark the channel as read prior to hiding it.

Optionally you can also clear the entire message history of that channel for the user. This way, when a new message is received, it will be the only one present in the channel.

// Hides the channel until a new message is added there
channelClient.hide(false).enqueue(result -> {
  if (result.isSuccess()) {
    // Channel is hidden
  } else {
    // Handle result.error()
  }
});

// Shows a previously hidden channel
channelClient.show().enqueue(result -> {
  if (result.isSuccess()) {
    // Channel is shown
  } else {
    // Handle result.error()
  }
});

// Hide the channel and clear the message history
channelClient.hide(true).enqueue(result -> {
  if (result.isSuccess()) {
    // Channel is hidden
  } else {
    // Handle result.error()
  }
});

You can still retrieve the list of hidden channels using the { "hidden" : true } query parameter.

© Getstream.io, Inc. All Rights Reserved.