/ SDKs / Android
SDKs
Chat SDKs Android v4
Chat SDKs Android
Chat SDKs
Android
Version 4

Ban and unban a user

Copy link

Operators of an open or group channel can remove any users that behave inappropriately in the channel using the ban feature. A banned user is immediately kicked out of the channel, but allowed to participate in the channel again after a set period of time has passed. Operators can ban and unban users using the following code.

Open channel

Copy link
OpenChannel.getChannel(CHANNEL_URL) { openChannel, e ->
    if (e != null) {
        // Handle error.
    }

    if (openChannel?.isOperator(SendbirdChat.currentUser) == true) {
        // Ban a user.
        openChannel?.banUser(USER, SECONDS) { e ->
            if (e != null) {
                // Handle error.
            }

            // The user is successfully banned from the channel.
            // You can display a message to let the user know they have been banned.
            // ...
        }

        // Unban a user.
        openChannel?.unbanUser(USER) { e ->
            if (e != null) {
                // Handle error.
            }

            // The user is successfully unbanned.
            // You can display a message to let the user know they have been unbanned.
            // ...
        }
    }
}

Group channel

Copy link
GroupChannel.getChannel(CHANNEL_URL) { groupChannel, e ->
    if (e != null) {
        // Handle error.
    }

    if (groupChannel?.myRole == Role.OPERATOR) {
        // Ban a user.
        groupChannel?.banUser(USER, DESCRIPTION, SECONDS) { e ->
            if (e != null) {
                // Handle error.
            }

            // The user is successfully banned from the channel.
            // You can display a message to let the user know they have been banned.
            // ...
        }

        // Unban a user.
        groupChannel?.unbanUser(USER) { e ->
            if (e != null) {
                // Handle error.
            }

            // The user is successfully unbanned.
            // You can display a message to let the user know they have been unbanned.
            // ...
        }
    }
}

Note: For both open and group channels, you can also pass the unique ID of a user as a parameter instead of USER to ban or unban using the banUser() and unbanUser() methods. These methods perform the same actions as the banUser() and unbanUser() methods.