Android
Channel Metadata

Channel Metadata

With metadata and metacounter, you can store additional information within a channel. Two data types allow you to store a Map of key-value pairs in a channel object. When you need an integer with atomic increasing and decreasing operations, use the metacounter instead of the metadata.

Use cases for the metadata and metacounters can be tracking the number of likes, the background color or a long description of a channel, which can each be fetched and rendered into the UI.


Create, update, retrieve, and delete a metadata

A metadata is a Map<String, String> that can be stored within a channel. Its uses are flexible, allowing to customize a channel to fit you and your users' needs.

Create

To store a metadata into a channel, create a Map<String, String>, add key-value pairs to it, and then pass it as a parameter when calling the createMetaData() method. You can store multiple key-value pairs 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 process for updating a metadata is identical with the creation. Values are updated for existing keys, while new key-value pairs 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

To get stored key-value pairs in a metadata, create a List of keys to retrieve, and then pass it as a parameter to the getMetaData() method. A Map<String, String> containing the corresponding key-value pairs is returned through the onResult() callback.

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

The process for deleting a metadata is as simple as creating one.

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

Create, update, increase, decrease, retrieve, and delete a metacounter

A metacounter is a Map<String, Integer> that can be stored within a channel object. Its primary uses are to track and update discrete indicators within a channel.

Create

To store a metacounter into a channel, create a Map<String, Integer>, add key-value pairs to it, and then pass it as a parameter when calling the createMetaCounter() method. You can store multiple key-value pairs 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 process for updating a metacounter is identical with the creation. Values are updated for existing keys, while new key-value pairs 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

To increase the values of key-value pairs in a metacounter, create a Map of keys to increase, and then pass it as a parameter to the increaseMetaCounters() method, which increments the corresponding keys 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

To decrease the values of key-value pairs in a metacounter, create a Map of keys to decrease, and then pass it as a parameter to the decreaseMetaCounters() method, which decrements the corresponding keys 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

To get stored key-value pairs in a metacounter, create a List of keys to retrieve, and then pass it as a parameter to the getMetaCounters() method. A Map<String, Integer> which contains the corresponding key-value pairs is returned through the onResult() callback.

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

The process for deleting a metacounter is as simple as creating one.

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