User Metadata

With a user metadata, you can store additional information to a user. The user metadata allows you to store a Map of String key-value pairs in a user instance. Use cases for 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 is stored into a user. Its uses are very flexible, allowing to customize a user to fit you and users' needs.

Create

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

User user = SendBird.getCurrentUser();

HashMap<String, String> data = new HashMap<String, String>();
data.put("phone", "001-0002-0003");
data.put("email", "sales@sendbird.com");

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.

User user = SendBird.getCurrentUser();

HashMap<String, String> data = new HashMap<String, String>();
data.put("phone", "001-0002-0004");
data.put("email", "engineering@sendbird.com");

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

Retrieve

To get the stored user 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("phone");
keys.add("email");

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.

User user = SendBird.getCurrentUser();

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