please select
  • UIKit
  • SDK
  • Server APIs
Chat/
SDK/
Android/
User/
SDK
  • Install Chat SDK
  • Initialize Chat SDK
  • Login and Logout
  • Message
    • Overview
    • Send a Message
    • Receive a Message
    • Retrieve Messages
    • Forward Messages
    • Modify a Message
    • Insert a Message
    • Delete Messages
    • Clear History Messages
    • Recall a Message
    • Send an Online Message
    • Message Read Receipt
    • Query Messages
    • Mentions
    • Targeted Group Message
    • Do not Notify
    • Key-Value Extensions
    • Reactions
    • Translation
    • Pin Messages
  • Conversation
    • Overview
    • Conversation List
    • Get Conversations
    • Unread Count
    • Pin Conversations
    • Delete Conversations
    • Draft
    • Mark
    • Conversation Group
  • Group
    • Overview
    • Manage Group
    • Profile
    • Manage Members
    • Member Profile
    • Attribute
    • Counter
  • Community and Topic
    • Manage Community
    • Permission Group
  • User
    • User Profile
    • User Status
    • Manage Friends
    • Friend Group
    • Block Lists
    • Follow
  • Local Search
    • Search Messages
    • Search Friends
    • Search Groups
    • Search Group Members
  • Signaling
  • API Reference
    • Java
  • Guideline for Beginners
  • Console Guide
    • Creating and Upgrading an Application
    • Basic Configuration
    • Feature Configuration
    • Account Management
    • Group Management
    • Webhook Configuration
  • Product Introduction
    • Message Management
      • One-to-One Message
      • Message Storage
      • Offline Push
      • Group Message
      • Message Formats
    • Account System
      • Login Authentication
      • Online Status Management
    • Group Related
      • Group System
      • Group Management
    • User Profile and Relationship Chain
      • Profile Management
      • Relationship Chain Management
  • Purchase Guide
    • Billing Overview
    • Pricing
  • Error Codes

Block Lists

Feature Description

To block a user's messages, add the user to the blocklist.

Blocklists

Blocking a user

Call addToBlackList (Android / iOS and Mac / Windows) to add a user to the blocklist, that is, block the user. If you have called addFriendListener to add a listener, blocking a user will trigger the onBlackListAdded callback.
Note:
1. If users A and B are friends, after either user A or user B is blocked by the other user, the two-way friend relationship will be terminated by default, and they cannot start a conversation with or send a friend request to each other.
2. If you want to keep users' friend relationship after being blacklisted,you can log in to the Console, and edit the Block configuration, toggle path: Applications > Your App > Chat > Configuration > Friends and Relationship Chains > Block configuration.
By default, a blocked user does not know that he/she is "blocked". After the user sends a message, the error code indicating that he/she has been blocked will not be returned. To have the "You have been blocked by the user" error message returned after a blocked user sends a message, you can log in to the Console and disable Blocklist Check. Then the SDK will report error code 20007 after a blocked user sends a message. The configuration path is: Applications > Your App > Chat > Configuration > Login and Message > Blocklist Check.
Sample code:
Android
iOS and macOS
Windows
List<String> userIDList = new ArrayList<>();
userIDList.add("user1");
userIDList.add("user2");
V2TIMManager.getFriendshipManager().addToBlackList(userIDList, new V2TIMValueCallback<List<V2TIMFriendOperationResult>>() {
@Override
public void onSuccess(List<V2TIMFriendOperationResult> v2TIMFriendOperationResults) {
// User blocked successfully
}

@Override
public void onError(int code, String desc) {
// Failed to block the user
}
});

// Listen for the notification of a user added to the blocklist
V2TIMManager.getFriendshipManager().addFriendListener(new V2TIMFriendshipListener() {
@Override
public void onBlackListAdd(List<V2TIMFriendInfo> infoList) {
// A user was added to the blocklist.
}
});
// Block a user
[[V2TIMManager sharedInstance] addToBlackList:@[@"user1", @"user2"] succ:^(NSArray<V2TIMFriendOperationResult *> *resultList) {
// User blocked successfully
} fail:^(int code, NSString *desc) {
// Failed to block the user
}];

// Listen for the notification of a user added to the blocklist
[[V2TIMManager sharedInstance] addFriendListener:self];
- (void)onBlackListAdded:(NSArray<V2TIMFriendInfo *>*)infoList {
// A user was added to the blocklist.
}
template <class T>
class ValueCallback final : public V2TIMValueCallback<T> {
public:
using SuccessCallback = std::function<void(const T&)>;
using ErrorCallback = std::function<void(int, const V2TIMString&)>;

ValueCallback() = default;
~ValueCallback() override = default;

void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
success_callback_ = std::move(success_callback);
error_callback_ = std::move(error_callback);
}

void OnSuccess(const T& value) override {
if (success_callback_) {
success_callback_(value);
}
}
void OnError(int error_code, const V2TIMString& error_message) override {
if (error_callback_) {
error_callback_(error_code, error_message);
}
}

private:
SuccessCallback success_callback_;
ErrorCallback error_callback_;
};

V2TIMStringVector userIDList;
userIDList.PushBack(u8"user1");
userIDList.PushBack(u8"user2");

auto callback = new ValueCallback<V2TIMFriendOperationResultVector>{};
callback->SetCallback(
[=](const V2TIMFriendOperationResultVector& friendOperationResultList) {
// User blocked successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to block the user
delete callback;
});

V2TIMManager::GetInstance()->GetFriendshipManager()->AddToBlackList(userIDList, callback);

// Listen for the notification of a user added to the blocklist
class FriendshipListener final : public V2TIMFriendshipListener {
public:
void OnBlackListAdded(const V2TIMFriendInfoVector& infoList) override {
// A user was added to the blocklist.
}
// Other members …
};
// Add a relationship chain event listener. Keep `friendshipListener` valid before the listener is removed to ensure event callbacks are received.
FriendshipListener friendshipListener;
V2TIMManager::GetInstance()->GetFriendshipManager()->AddFriendListener(&friendshipListener);

Unblocking a user

Call deleteFromBlackList (Android / iOS and Mac / Windows) to remove a user from the blocklist, that is, unblock the user, after which the user can send a friend request and start a conversation. If you have called addFriendListener to add a listener, unblocking a user will trigger the onBlackListDeleted callback.
Sample code:
Android
iOS and macOS
Windows
List<String> userIDList = new ArrayList<>();
userIDList.add("user1");
userIDList.add("user2");
V2TIMManager.getFriendshipManager().deleteFromBlackList(userIDList, new V2TIMValueCallback<List<V2TIMFriendOperationResult>>() {
@Override
public void onSuccess(List<V2TIMFriendOperationResult> v2TIMFriendOperationResults) {
// User unblocked successfully
}

@Override
public void onError(int code, String desc) {
// Failed to unblock the user
}
});

// Listen for the notification of a user removed from the blocklist
V2TIMManager.getFriendshipManager().addFriendListener(new V2TIMFriendshipListener() {
@Override
public void onBlackListDeleted(List<String> userList) {
// A user was removed from the blocklist.
}
});
// Unblock a user
[[V2TIMManager sharedInstance] deleteFromBlackList:@[@"user1", @"user2"] succ:^(NSArray<V2TIMFriendOperationResult *> *resultList) {
// User unblocked successfully
} fail:^(int code, NSString *desc) {
// Failed to unblock the user
}];

// Listen for the notification of a user removed from the blocklist
[[V2TIMManager sharedInstance] addFriendListener:self];
- (void)onBlackListDeleted:(NSArray*)userIDList {
// A user was removed from the blocklist.
}

template <class T>
class ValueCallback final : public V2TIMValueCallback<T> {
public:
using SuccessCallback = std::function<void(const T&)>;
using ErrorCallback = std::function<void(int, const V2TIMString&)>;

ValueCallback() = default;
~ValueCallback() override = default;

void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
success_callback_ = std::move(success_callback);
error_callback_ = std::move(error_callback);
}

void OnSuccess(const T& value) override {
if (success_callback_) {
success_callback_(value);
}
}
void OnError(int error_code, const V2TIMString& error_message) override {
if (error_callback_) {
error_callback_(error_code, error_message);
}
}

private:
SuccessCallback success_callback_;
ErrorCallback error_callback_;
};

V2TIMStringVector userIDList;
userIDList.PushBack(u8"user1");
userIDList.PushBack(u8"user2");

auto callback = new ValueCallback<V2TIMFriendOperationResultVector>{};
callback->SetCallback(
[=](const V2TIMFriendOperationResultVector& friendOperationResultList) {
// User unblocked successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to unblock the user
delete callback;
});

V2TIMManager::GetInstance()->GetFriendshipManager()->DeleteFromBlackList(userIDList, callback);

// Listen for the notification of a user removed from the blocklist
class FriendshipListener final : public V2TIMFriendshipListener {
public:
void OnBlackListAdded(const V2TIMFriendInfoVector& infoList) override {
// A user was removed from the blocklist.
}
// Other members …
};
// Add a relationship chain event listener. Keep `friendshipListener` valid before the listener is removed to ensure event callbacks are received.
FriendshipListener friendshipListener;
V2TIMManager::GetInstance()->GetFriendshipManager()->AddFriendListener(&friendshipListener);

Getting a blocklist

Call getBlackList (Android / iOS and Mac / Windows) to view how many users have been blocked and manage them.
Sample code:
Android
iOS and macOS
Windows
V2TIMManager.getFriendshipManager().getBlackList(new V2TIMValueCallback<List<V2TIMFriendInfo>>() {
@Override
public void onSuccess(List<V2TIMFriendInfo> v2TIMFriendInfos) {
// Blocklist obtained successfully
}

@Override
public void onError(int code, String desc) {
// Failed to obtain the blocklist
}
});
// Obtain a blocklist
[[V2TIMManager sharedInstance] getBlackList:^(NSArray<V2TIMFriendInfo *> *infoList) {
// Blocklist obtained successfully
} fail:^(int code, NSString *desc) {
// Failed to obtain the blocklist
}];
template <class T>
class ValueCallback final : public V2TIMValueCallback<T> {
public:
using SuccessCallback = std::function<void(const T&)>;
using ErrorCallback = std::function<void(int, const V2TIMString&)>;

ValueCallback() = default;
~ValueCallback() override = default;

void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {
success_callback_ = std::move(success_callback);
error_callback_ = std::move(error_callback);
}

void OnSuccess(const T& value) override {
if (success_callback_) {
success_callback_(value);
}
}
void OnError(int error_code, const V2TIMString& error_message) override {
if (error_callback_) {
error_callback_(error_code, error_message);
}
}

private:
SuccessCallback success_callback_;
ErrorCallback error_callback_;
};

auto callback = new ValueCallback<V2TIMFriendInfoVector>{};
callback->SetCallback(
[=](const V2TIMFriendInfoVector& friendInfoList) {
// Blocklist obtained successfully
delete callback;
},
[=](int error_code, const V2TIMString& error_message) {
// Failed to obtain the blocklist
delete callback;
});

V2TIMManager::GetInstance()->GetFriendshipManager()->GetBlackList(callback);