/ SDKs / iOS
SDKs
Chat SDKs iOS v4
Chat SDKs iOS
Chat SDKs
iOS
Version 4

Add extra data to a message

Copy link

You can add extra data, such as metadata and sorted_metaarray, to a message in an open or group channel. By storing one or more key-value items to a message, you can use the data to implement various actions in a channel. Based on the items, you can measure user engagement in conversations or count how many times a message has been copied by users in the channel. As shown in the code below, you can add, update, or delete these key-value items anytime. To retrieve the key-values items of a message, call message.metaArrays.

Note: To learn more about the different options for storing additional information about a user, channel, or message, go to the overview page under Channel.

// When a message has been successfully sent to a channel,
// create a map of keys.
channel.createMessageMetaArrayKeys(message: MESSAGE, keys: ["referees", "games"]) { message, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

// Then add values to those keys.
let valuesToAdd = [
    "referees": ["John", "Brandon", "Harry", "Jay"],
    "games": ["soccer", "baseball", "basketball"]
]

channel.addMessageMetaArrayValues(message: MESSAGE, keyValues: valuesToAdd) { message, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

// You can remove the existing values of specific items
// by specifying their keys.
let valuesToRemove = [
    "referees": ["Brandon", "Jay"]
]

channel.removeMessageMetaArrayValues(message: MESSAGE, keyValues: valuesToRemove) { message, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

// Delete items by specifying their keys.
channel.deleteMessageMetaArrayKeys(message: MESSAGE, keys: ["referees", "games"]) { message, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Note: Every Sendbird application has a different limit on how many key-value items you can add to a single message, as well as the maximum number of values an item can have. If you wish to learn more about these limits, contact our sales team.