addToBlackList
(Java / Swift / Objective-C / C++) 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.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.List<String> userIDList = new ArrayList<>();userIDList.add("user1");userIDList.add("user2");V2TIMManager.getFriendshipManager().addToBlackList(userIDList, new V2TIMValueCallback<List<V2TIMFriendOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMFriendOperationResult> v2TIMFriendOperationResults) {// User blocked successfully}@Overridepublic void onError(int code, String desc) {// Failed to block the user}});// Listen for the notification of a user added to the blocklistV2TIMManager.getFriendshipManager().addFriendListener(new V2TIMFriendshipListener() {@Overridepublic void onBlackListAdd(List<V2TIMFriendInfo> infoList) {// A user was added to the blocklist.}});
V2TIMManager.shared.addToBlackList(userIDList: ["user1", "user2"]) { resultList inprint( "addToBlackList succ")} fail: { code, desc inprint( "addToBlackList fail, \\(code), \\(desc)")}V2TIMManager.shared.addFriendListener(listener: self)func onBlackListAdded(infoList: Array<V2TIMFriendInfo>) {print( infoList)}
// 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to block the userdelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->AddToBlackList(userIDList, callback);// Listen for the notification of a user added to the blocklistclass 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);
deleteFromBlackList
(Java / Swift / Objective-C / C++) 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.List<String> userIDList = new ArrayList<>();userIDList.add("user1");userIDList.add("user2");V2TIMManager.getFriendshipManager().deleteFromBlackList(userIDList, new V2TIMValueCallback<List<V2TIMFriendOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMFriendOperationResult> v2TIMFriendOperationResults) {// User unblocked successfully}@Overridepublic void onError(int code, String desc) {// Failed to unblock the user}});// Listen for the notification of a user removed from the blocklistV2TIMManager.getFriendshipManager().addFriendListener(new V2TIMFriendshipListener() {@Overridepublic void onBlackListDeleted(List<String> userList) {// A user was removed from the blocklist.}});
// Unblock a userV2TIMManager.shared.deleteFromBlackList(userIDList: ["user1"]) { resultList inprint( "deleteFromBlackList succ")} fail: { code, desc inprint( "deleteFromBlackList fail, \\(code), \\(desc)")}V2TIMManager.shared.addFriendListener(listener: self)func onBlackListDeleted(userIDList: Array<String>) {print( userIDList)}
// 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to unblock the userdelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->DeleteFromBlackList(userIDList, callback);// Listen for the notification of a user removed from the blocklistclass 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);
getBlackList
(Java / Swift / Objective-C / C++) to view how many users have been blocked and manage them.V2TIMManager.getFriendshipManager().getBlackList(new V2TIMValueCallback<List<V2TIMFriendInfo>>() {@Overridepublic void onSuccess(List<V2TIMFriendInfo> v2TIMFriendInfos) {// Blocklist obtained successfully}@Overridepublic void onError(int code, String desc) {// Failed to obtain the blocklist}});
V2TIMManager.shared.getBlackList { infoList ininfoList.forEach { item in// V2TIMFriendInfoprint( item.description)}} fail: { code, desc inprint( "getBlackList fail, \\(code), \\(desc)")}
// 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 successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to obtain the blocklistdelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->GetBlackList(callback);
Was this page helpful?