Android
User Metadata

User Metadata

With a user metadata, you can store additional information to a user. Use cases for the metadata are such as phone number, email or a long description of the user, which can each be fetched and rendered into the UI.


Create, update, retrieve, and delete a user metadata

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

Create

To store a user metadata into a User object, 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
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 process for updating a user metadata is identical with the creation. Values are updated for existing keys, while new key-value pairs 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

To get stored key-value pairs in a user 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() method.

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

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

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