Android
User Channel Metadata

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 a String and its length must be no larger than 190 characters. For channel metacounter, the value must be an 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. A user metadata is a Map<String, String> and it can be stored into a User object.

Create

To store a user metadata into a User object, add key-value items after creating a 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.

Light Color Skin
Copy
User user = SendBird.getCurrentUser();

HashMap<String, String> data = new HashMap<String, String>();
data.put("key1", "value1");
data.put("key2", "value2");

user.createMetaData(data, new User.MetaDataHandler() {
    @Override
    public void onResult(Map<String, String> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Update

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.

Light Color Skin
Copy
User user = SendBird.getCurrentUser();

HashMap<String, String> data = new HashMap<String, String>();
data.put("key1", "valueToUpdate1");
data.put("key2", "valueToUpdate2");

user.updateMetaData(data, new User.MetaDataHandler() {
    @Override
    public void onResult(Map<String, String> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Retrieve

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

Light Color Skin
Copy
List<String> keys = new ArrayList<String>();
keys.add("key1");
keys.add("key2");

user.getMetaData(keys, new User.MetaDataHandler() {
    @Override
    public void onResult(Map<String, String> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Delete

You can delete a user metadata as below.

Light Color Skin
Copy
User user = SendBird.getCurrentUser();

user.deleteMetaData("key1", new User.MetaDataHandler() {
    @Override
    public void onResult(SendBirdException e) {
        if (e != null) {    // Error.
            return;
                }
    }
});

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. A channel metadata is a Map<String, String> and it can be stored into a Channel object.

Create

To store a channel metadata into a Channel object, add key-value items after creating a 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.

Light Color Skin
Copy
HashMap<String, String> data = new HashMap<String, String>();
data.put("key1", "value1");
data.put("key2", "value2");

channel.createMetaData(data, new BaseChannel.MetaDataHandler() {
    @Override
    public void onResult(Map<String, String> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Update

The procedure to update a 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.

Light Color Skin
Copy
HashMap<String, String> data = new HashMap<String, String>();
data.put("key1", "valueToUpdate1"); // Update an existing item with a new value.
data.put("key2", "valueToUpdate2"); // Update an existing item with a new value.
data.put("key3", "valueToAdd3");    // Add a new key-value item.

channel.updateMetaData(data, new BaseChannel.MetaDataHandler() {
    @Override
    public void onResult(Map<String, String> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Retrieve

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

Light Color Skin
Copy
List<String> keys = new ArrayList<String>();
keys.add("key1");
keys.add("key2");

channel.getMetaData(keys, new BaseChannel.MetaDataHandler() {
    @Override
    public void onResult(Map<String, String> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Delete

You can delete a channel metadata as below.

Light Color Skin
Copy
channel.deleteMetaData("key1", new BaseChannel.MetaDataHandler() {
    @Override
    public void onResult(SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

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. A channel metacounter is a Map<String, Integer> and it can be stored into a Channel object.

Note : A 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 a channel metacounter into a Channel object, add key-value items after creating a Map<String, Integer>, and pass it as an argument to a parameter when calling the createMetaCounter() method. You can put multiple key-value items in the map.

Light Color Skin
Copy
HashMap<String, Integer> counters = new HashMap<String, Integer>();
counters.put("key1", 1);
counters.put("key2", 2);

channel.createMetaCounters(counters, new BaseChannel.MetaCounterHandler() {
    @Override
    public void onResult(Map<String, Integer> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Update

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

Light Color Skin
Copy
HashMap<String, Integer> data = new HashMap<String, Integer>();
data.put("key1", 3);    // Update 1 to 3
data.put("key2", 4);    // Update 2 to 4
data.put("key3", 0);    // Add a new key-value item.

channel.updateMetaCounters(data, new BaseChannel.MetaCounterHandler() {
    @Override
    public void onResult(Map<String, Integer> map, SendBirdException e) {
        if (e != null) {        // Error.
            return;
        }
    }
});

Increase

You can increase values in a channel metacounter by passing a 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.

Light Color Skin
Copy
HashMap<String, Integer> counters = new HashMap<String, Integer>();
counters.put("key1", 2); // Increase by 2.
counters.put("key2", 3); // Increase by 3.

channel.increaseMetaCounters(counters, new BaseChannel.MetaCounterHandler() {
    @Override
    public void onResult(Map<String, Integer> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Decrease

You can decrease values in a channel metacounter by passing a 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.

Light Color Skin
Copy
HashMap<String, Integer> counters = new HashMap<String, Integer>();
counters.put("key1", 3); // Decrease by 3.
counters.put("key2", 4); // Decrease by 4.

channel.decreaseMetaCounters(counters, new BaseChannel.MetaCounterHandler() {
    @Override
    public void onResult(Map<String, Integer> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Retrieve

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

Light Color Skin
Copy
List<String> keys = new ArrayList<String>();
keys.add("key1");
keys.add("key2");

channel.getMetaCounters(keys, new BaseChannel.MetaCounterHandler() {
    @Override
    public void onResult(Map<String, Integer> map, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Delete

You can delete a channel metacounter as below.

Light Color Skin
Copy
channel.deleteMetaCounters("key1", new BaseChannel.MetaCounterHandler() {
    @Override
    public void onResult(SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});