Block Users

Learn how to block users from sending messages to the logged-in user.

Block Users

In other words, as a logged-in user, how do I block a user from sending me messages?

You can block users using the blockUsers() method. Once any user is blocked, all the communication to and from the respective user will be completely blocked. You can block multiple users in a single operation. The blockUsers() method takes a List<String> as a parameter which holds the list of UIDs to be blocked.

List<String > uids = new ArrayList<>();
uids.add("UID1");
uids.add("UID2");
uids.add("UID3");
CometChat.blockUsers(uids, new CometChat.CallbackListener<HashMap<String, String>>() {
  @Override
    public void onSuccess(HashMap<String, String> resultMap) {
        // Handle block users success.
  }

  @Override
    public void onError(CometChatException e) {
        // Handle block users failure
  }
});
val uids = ArrayList<String>()
  uids.add("UID1")
  uids.add("UID2")
  uids.add("UID3")

 CometChat.blockUsers(uids,object:CometChat.CallbackListener<HashMap<String,     String>>() {
            
    override fun onSuccess(resultMap: HashMap<String, String>) {
                // Handle unblock users success.
     }

    override fun onError(e: CometChatException) {
                // Handle unblock users failure
     }
})

In the onSuccess() callback, you receive a HashMap which contains UIDs as the keys and success or fail as the value based on if the block operation for the UID was successful or not.

Unblock Users

In other words, as a logged-in user, how do I unblock a user I previously blocked?

You can unblock the already blocked users using the unblockUsers() method. You can unblock multiple users in a single operation. The unblockUsers() method takes a List<String> as a parameter which holds the list of UIDs to be unblocked.

List<String > uids = new ArrayList<>();
uids.add("UID1");
uids.add("UID2");
uids.add("UID3");
CometChat.unblockUsers(uids, new CometChat.CallbackListener<HashMap<String, String>>() {
  @Override
    public void onSuccess(HashMap<String, String> resultMap) {
        // Handle unblock users success.
  }

  @Override
    public void onError(CometChatException e) {
        // Handle unblock users failure
  }
});
val uids = ArrayList<String>()
        uids.add("UID1")
        uids.add("UID2")
        uids.add("UID3")

        CometChat.unblockUsers(uids,object:CometChat.CallbackListener<HashMap<String, String>>() {

        override fun onSuccess(resultMap: HashMap<String, String>) {
                // Handle unblock users success.
        }

        override fun onError(e: CometChatException) {
                // Handle unblock users failure
        }
  })

In the onSuccess() callback, you receive a HashMap which contains UIDs as the keys and success or fail as the value based on if the unblock operation for the UID was successful or not.

Get list of blocked users

In other words, as a logged-in user, how do I get a list of all users I've blocked?

In order to fetch the list of blocked users, you can use the BlockedUsersRequest class. To use this class i.e to create an object of the BlockedUsersRequest class, you need to use the BlockedUsersRequestBuilder class. The BlockedUsersRequestBuilder class allows you to set the parameters based on which the blocked users are to be fetched.

The BlockedUsersRequestBuilder class allows you to set the below parameters:

  1. setLimit(int limit) - This method sets the limit i.e. the number of blocked users that should be fetched in a single iteration.
BlockedUsersRequest blockedUsersRequest = new BlockedUsersRequest.BlockedUsersRequestBuilder()
.setLimit(10)
.build();
  1. setSearchKeyword(String searchString) - This method allows you to set the search string based on which the blocked users are to be fetched.
BlockedUsersRequest blockedUsersRequest = new BlockedUsersRequest.BlockedUsersRequestBuilder()
.setSearchKeyword("abc")
.build();
  1. setDirection(String direction) - This can hold one of the below values:
    a. BlockedUsersRequest.DIRECTION_BLOCKED_BY_ME - This will return the list of users blocked by the logged-in user.
    b. BlockedUsersRequest.DIRECTION_HAS_BLOCKED_ME - This will return the list of users that have blocked the logged-in user.
    c. BlockedUsersRequest.DIRECTION_BOTH - This will return the list of users for both the above cases. This is the default value for the direction variable if it is not set.
BlockedUsersRequest blockedUsersRequest = new BlockedUsersRequest.BlockedUsersRequestBuilder()
.setDirection(BlockedUsersRequest.DIRECTION_BLOCKED_BY_ME)
.build();

Finally, once all the parameters are set in the builder class, you need to call the build() method to get the object of the BlockedUsersRequest class.

Once you have the object of the BlockedUsersRequest class, you need to call the fetchNext() method. Calling this method will return a list of User objects containing n number of blocked users where N is the limit set in the builder class.

BlockedUsersRequest blockedUsersRequest = new BlockedUsersRequest.BlockedUsersRequestBuilder().setLimit(10).build();

blockedUsersRequest.fetchNext(new CometChat.CallbackListener<List<User>>() {
  @Override
    public void onSuccess(List<User> users) {
    for(User user : users){
      Log.e(TAG, user.getUid());
    }
  }

  @Override
    public void onError(CometChatException e) {
    Log.e(TAG, e.getMessage());
  }
});
val blockedUsersRequest = BlockedUsersRequest.BlockedUsersRequestBuilder().setLimit(10).build()

blockedUsersRequest.fetchNext(object : CometChat.CallbackListener<List<User>>() {
      override fun onSuccess(users: List<User>) {
          for (user in users) {
                    Log.e(TAG, user.getUid())
           }
      }

      override fun onError(e: CometChatException) {
                Log.e(TAG, e.message)
      }
})

Did this page help you?