Channel Metadata

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

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


Create, update, retrieve, and delete a metadata

Metadata is a Map<String, String> that is stored within a channel. Its uses are very flexible, allowing to customize a channel to fit you and users' needs.

Create

To store metadata into a channel, create a Map, and then pass it as an argument when calling createMetaData(). You can store multiple key-value pairs in the map.

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 metadata is identical with the creation. Values are updated for existing keys, while new key-value pairs added.

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

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 metadata, create Collection of keys, and then pass it as an argument to getMetaData(). The callback onResult() returns a Map<String, String> containing the corresponding key-value pairs.

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.

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

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

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

Create

To store metacounter into a channel, create a Map, and then pass it as an argument when calling createMetaCounter(). You can store multiple key-value pairs in the map.

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;
        }
    }
});

Increase

The increase and decrease operations work similarly to getting metacounters, as described above. Create a Collection of keys to pass to increaseMetaCounters(), which increments the corresponding metacounters by the number you've specified.

HashMap<String, Integer> counters = new HashMap<String, Integer>();
counters.put("key1", 2); // Increases by 2.
counters.put("key2", 3); // Increases by 3.

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

Decrease

Pass a Collection of keys to decreaseMetaCounters(), which decrements the metacounters by the number you've specified.

HashMap<String, Integer> counters = new HashMap<String, Integer>();
counters.put("key1", 3); // Decreases by 3.
counters.put("key2", 4); // Decreases 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 metacounters, create a Collection of keys, and then pass it as an argument to getMetaCounters(). The callback onResult() returns a Map<String, Integer> containing the corresponding key-value pairs.

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.

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