Chat Android v4
Chat Android
Chat
Android
Home
/
Chat
/
Android
This is the new Docs for Chat SDK v4 beta for Android. To see the previous Docs, click here.

User & channel metadata

With metadata and metacounter which consist of key-value items, you can store additional information to users and channels. This page explains how to manage user metadata, channel metadata, and channel metacounter.

Note : User metadata, channel metadata, and channel metacounter can each have up to 5 key-value items. The key’s length must be no larger than 128 characters. For user metadata and channel metadata, the value must be String and its length must be no larger than 190 characters. For channel metacounter, the value must be Integer.


User metadata

You can store additional information to users such as phone number, email or a long description of a user with user metadata, which can be fetched or rendered into the UI. User metadata is Map<String, String> and it can be stored into a User object.

Create user metadata

To store a user metadata into a User object, add key-value items after creating Map<String, String>, and pass it as an argument to a parameter when calling the createMetaData() method. You can put multiple key-value items in the map.

val user = SendbirdChat.currentUser
val data = mapOf("key1" to "value1", "key2" to "value2")
user.createMetaData(data) { metaDataMap, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Update user metadata

The procedure to update a user metadata is the same as creating a user metadata. Values of existing keys will be updated and values of new keys will be added.

val user = SendbirdChat.currentUser
val data = mapOf("key1" to "valueToUpdate1", "key2" to "valueToUpdate2")
user.updateMetaData(data) { metaDataMap, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Delete user metadata

You can delete a user metadata as below.

val user = SendbirdChat.currentUser
user.deleteMetaData("key1") { e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Channel metadata

You can store additional information to channels such as background color or a long description of a channel with channel metadata, which can be fetched or rendered into the UI. Channel metadata is Map<String, String> and it can be stored into a Channel object.

Create channel metadata

To store channel metadata into a Channel object, add key-value items after creating Map<String, String>, and pass it as an argument to a parameter when calling the createMetaData() method. You can put multiple key-value items in the map.

val data = mapOf("key1" to "value1", "key2" to "value2")
channel.createMetaData(data) { map, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Update channel metadata

The procedure to update channel metadata is the same as creating a channel metadata. Values of existing keys will be updated and values of new keys will be added.

val data = mapOf(
    "key1" to "valueToUpdate1", // Update an existing item with a new value.
    "key2" to "valueToUpdate2", // Update an existing item with a new value.
    "key3" to "valueToAdd3"     // Add a new key-value item.
)
channel.updateMetaData(data) { map, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Retrieve channel metadata

You can retrieve channel metadata by creating List of keys to retrieve and passing it as an argument to a parameter in the getMetaData() method. The Map<String, String> collection will return key-value items through the onResult() method.

val keys = listOf("key1", "key2")
channel.getMetaData(keys) { map, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Retrieve cached channel metadata

When Sendbird Chat SDK detects any of the create, read, update, and delete operations on the channel metadata, the SDK caches the metadata. The cached metadata is also updated whenever a channel list is fetched.

You can retrieve the cached metadata through the cachedMetaData() method without having to query the server.

val cachedMetaData = channel.cachedMetaData
val value = cachedMetaData[key] // The key should be a string.

Delete channel metadata

You can delete channel metadata as below.

channel.deleteMetaData("key1") { e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Channel metacounter

You can store additional information to channels such as the tracking number of likes with channel metacounter, which can be fetched or rendered into the UI. Channel metacounter is Map<String, Integer> and it can be stored into a Channel object.

Note : Channel metacounter is primarily used to track and update discrete indicators in a channel. Use channel metacounter instead of channel metadata when you need an integer with atomic increasing and decreasing operations.

Create

To store channel metacounter into a Channel object, add key-value items after creating Map<String, Integer>, and pass it as an argument to a parameter when calling the createMetaCounters() method. You can put multiple key-value items in the map.

val counters = mapOf("key1" to 1, "key2" to 2)
channel.createMetaCounters(counters) { map, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Update

The procedure to update channel metacounter is the same as creating channel metacounter. Values of existing keys will be updated and values of new keys will be added.

val counters = mapOf(
    "key1" to 3,    // Update 1 to 3.
    "key2" to 4,    // Update 2 to 4.
    "key3" to 0     // Add a new key-value item.
)
channel.updateMetaCounters(counters) { map, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Increase

You can increase values in a channel metacounter by passing Map of keys to increase as an argument to a parameter in the increaseMetaCounters() method. The values of corresponding keys in the metacounter will be incremented by the number you’ve specified.

val counters = mapOf(
    "key1" to 2,    // Increase by 2.
    "key2" to 3     // Increase by 3.
)
channel.increaseMetaCounters(counters) { map, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Decrease

You can decrease values in a channel metacounter by passing Map of keys to decrease as an argument to a parameter in the decreaseMetaCounters() method. The values of corresponding keys in the metacounter will be decremented by the number you’ve specified.

val counters = mapOf(
    "key1" to 3,    // Decrease by 3.
    "key2" to 4     // Decrease by 4.
)
channel.decreaseMetaCounters(counters) { map, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Retrieve

You can retrieve channel metacounter by creating List of keys to retrieve and passing it as an argument to a parameter in the getMetaCounters() method. A Map<String, Integer> collection will return through the onResult() method with corresponding key-value items.

val keys = listOf("key1", "key2")
channel.getMetaCounters(keys) { map, e ->
    if (e != null) {
        // Handle error.
    }

    ...
}

Delete

You can delete a channel metacounter as below.

channel.deleteMetaCounter("key1") { e ->
    if (e != null) {
        // Handle error.
    }

    ...
}