[Android] 2. API Documentation
The CMPManager
class provides methods to manage user consent for data processing and tracking. This documentation covers the main methods available for mobile app integration.
Initialization
Object UrlConfig(:UrlConfig)
Sets the URL configuration for the Consent Manager. Needs to be initialized with the value below, and passed to the getInstance
method.
Parameters:
-
id
: String - The CMP ID -
domain
: String - The domain for consent management -
language
: String - The language code (e.g., "EN") -
appName
: String - The name of your app
Example:
val urlConfig = UrlConfig(
id = "YOUR_CODE_ID_HERE",
domain = "delivery.consentmanager.net",
language = "EN",
appName = "CMDemoAppKotlin"
)
setActivity(activity: Activity)
Sets the Activitythat will present the consent layer. It should be a ComponentActivity
.
Parameters:
-
viewController
: ComponentActivity - The Activity where the consent layer will be presented.
Returns: None
Example:
CMPManager.shared.setPresentingViewController(self)
Consent Layer Management
checkIfConsentIsRequired(completion: (Boolean) -> Unit)
Checks if consent is required for the user. This will make a network call to our servers via the WebView created inside our SDK, which will send a message to our backend via JavaScript. Our backend will detect whether the device has a valid consent or not, and message will be sent back to the WebView, determining if it needs to be displayed or not, consuming one pageview in the process. Depending on the message returned, the SDK will interpret it and return its results via the completion handler.
Parameters:
-
completion
: (Boolean) -> Unit - A closure called with the result, eithertrue
orfalse
.
Returns: None
Example:
cmpManager.checkIfConsentIsRequired { needsConsent ->
toastMessage = "Needs Consent: $needsConsent"
}
checkWithServerAndOpenIfNecessary(completion: (Result<Unit>) -> Unit)
Checks with the server if consent is required and opens the consent layer if necessary. This will make a network call to our servers via the WebView created inside our SDK, consuming one pageview in the process. This network call will send a message to our backend via JavaScript, whih will detect whether the device has a valid consent or not, which will in turn determine whether the consent layer needs to be displayed or not.
Parameters:
-
completion
: A closure called when the operation completes, with the result, eithertrue
orfalse
.
Returns: None
Example:
cmpManager.checkWithServerAndOpenIfNecessary { result ->
result.onSuccess {
toastMessage = "Check and Open Consent Layer operation done successfully."
}.onFailure { error ->
toastMessage = "Check and Open Consent Layer operation failed with error: $error"
}
openConsentLayer(completion: (Result<Unit>) -> Unit)
Opens the consent layer. It makes a network call to our backend, consuming one pageview in the process. It will display the consent layer allowing the user to either Accept All or Reject All of the choices, or, depending on the CMP design, allowing them to control the consents in a more granular way.
Parameters:
-
completion
: A closure called when the operation completes, returning either a sucess or an error.
Returns: None
Example:
cmpManager.openConsentLayer { result ->
result.onFailure { error ->
toastMessage = "Error: ${error.message}"
}
}
Consent Status
exportCMPInfo()
Exports the current consent information stored on the device as a string. This method retrieves the consent string from the SharedPreferences area of the device, and returns it. Usually this information is passed to the importCMPInfo
method.
Returns: String - The exported consent information
Example:
val cmpInfo = CMPManager.shared.exportCMPInfo()
Log.d("Exported CMP info: \(cmpInfo)")
hasPurposeConsent(id: String)
Checks if consent has been given for a specific purpose and this consent is stored on the device. It checks the SharedPreferences area for the consents accepted or rejected, and filter the ID passed as a parameter, returning true if the consent was accepted or false otherwise.
Parameters:
-
id
: String - The ID of the purpose to check
Returns: Boolean
- True
if consent is given, false
otherwise
Example:
val hasPurposeConsent = cmpManager.hasPurposeConsent(id: "c53")
Log.d("Has consent for purpose c53: \(hasPurposeConsent)")
hasUserChoice()
Checks if the user has made a choice regarding consents and this consent is stored on the device. It means that the user either accepted all consents, rejected all of them, or made a mixed choice of rejected and accepted consents, depending on the CMP design, which might allow the users to allow some of the consents and reject others. This information will only be up-to-date after the consent is properly persisted in the SharedPreferences area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer
, acceptAll
or rejectAll
, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice
, to make sure that the information is up-to-date.
Returns: Boolean
- true
if the user has made a choice, false
otherwise
Example:
val hasChoice = cmpManager.hasUserChoice()
print("User has made a choice: \(hasChoice)")
hasVendorConsent(id: String)
Checks if consent has been given for a specific vendor and this consent is stored on the device, according to the CMP configurations. This information will only be up-to-date after the consent is properly persisted in the SharedPreferences area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer
, acceptAll
or rejectAll
, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice
, to make sure that the information is up-to-date.
Parameters:
-
id
: String - The ID of the vendor to check
Returns: Boolean
- True
if consent is given, false
otherwise
Example:
val hasVendorConsent = cmpManager.hasVendorConsent(id: "s2789")
Log.d("Has consent for vendor s2789: \(hasVendorConsent)")
Purpose and Vendor Management
getAllPurposesIDs()
Retrieves all purpose IDs stored on the device, according to the CMP configurations. This information will only be up-to-date after the consent is properly persisted in the SharedPreferences area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer
, acceptAll
or rejectAll
, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice
, to make sure that the information is up-to-date.
Returns: List<String> - A list of all purpose IDs
Example:
val allPurposes = cmpManager.getAllPurposesIDs()
Log.d("All purposes: \(allPurposes)")
getAllVendorsIDs()
Retrieves all vendor IDs stored on the device, according to the CMP configurations. This information will only be up-to-date after the consent is properly persisted in the SharedPreferences area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer
, acceptAll
or rejectAll
, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice
, to make sure that the information is up-to-date.
Returns: List<String> - A list of all vendor IDs
Example:
val allVendors = cmpManager.getAllVendorsIDs()
Log.d("All vendors: \(allVendors)")
getDisabledPurposesIDs()
Retrieves the IDs of all disabled purposes stored on the device, according to the CMP configurations and the user choices. If the user accepts all the consents, this will be empty. This information will only be up-to-date after the consent is properly persisted in the SharedPreferences area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer
, acceptAll
or rejectAll
, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice
, to make sure that the information is up-to-date.
Returns: List<String> - A list of disabled purpose IDs
Example:
val disabledPurposes = cmpManager.getDisabledPurposesIDs()
Log.d("Disabled purposes: \(disabledPurposes)")
getDisabledVendorsIDs()
Retrieves the IDs of all disabled vendors stored on the device, according to the CMP configurations. If the user accepts all the consents, this will be empty. This information will only be up-to-date after the consent is properly persisted in the SharedPreferences area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer
, acceptAll
or rejectAll
, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice
, to make sure that the information is up-to-date.
Returns: List<String> - A list of disabled vendor IDs
Example:
val disabledVendors = CMPManager.shared.getDisabledVendorsIDs()
Log.d("Disabled vendors: \(disabledVendors)")
getEnabledPurposesIDs()
Retrieves the IDs of all enabled purposes stored on the device, according to the CMP configurations. If the user rejects all the consents, this will be empty. This information will only be up-to-date after the consent is properly persisted in the SharedPreferences area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer
, acceptAll
or rejectAll
, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice
, to make sure that the information is up-to-date.
Returns: List<String> - A list of enabled purpose IDs
Example:
val enabledPurposes = cmpManager.getEnabledPurposesIDs()
Log.d("Enabled purposes: \(enabledPurposes)")
getEnabledVendorsIDs()
Retrieves the IDs of all enabled vendors stored on the device. If the user rejects all the consents, this will be empty. This information will only be up-to-date after the consent is properly persisted in the SharedPreferences area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer
, acceptAll
or rejectAll
, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice
, to make sure that the information is up-to-date.
Returns: List<String> - A list of enabled vendor IDs
Example:
val enabledVendors = cmpManager.getEnabledVendorsIDs()
Log.d("Enabled vendors: \(enabledVendors)")
Consent Modification
acceptAll(completion: (Result<Unit>) -> Unit)
Accepts consent for all purposes and vendors, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WebView, which will trigger the acceptance of all the consents, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
Parameters:
-
completion
: A closure called when the operation completes, returning failure or success.
Returns: None
Example:
cmpManager.acceptAll { result ->
result.onSuccess {
toastMessage = "All consents accepted"
}.onFailure { error ->
toastMessage = "Error: ${error.message}"
}
}
acceptPurposes(purposes: List<String>, updatePurpose: Boolean, completion: (Result<Unit>) -> Unit)
Accepts consent for specified purposes, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WebView, which will trigger the acceptance of the determined purposes passed as a parameter, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
Parameters:
-
purposes
: List<String> - A list of purpose IDs to accept -
updatePurpose
: Boolean - Whether to update related purposes -
completion
: A closure called when the operation completes, returning either a failure or a success
Returns: None
Example:
cmpManager.acceptPurposes(listOf("c52", "c53"), true) { result ->
result.onSuccess {
toastMessage = "Purposes enabled"
}.onFailure { error ->
toastMessage = "Error: ${error.message}"
}
}
acceptVendors(vendors: List<String>, completion: (Result<Unit>) -> Unit)
Accepts consent for specified vendors, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WebView, which will trigger the acceptance of the determined vendors passed as a parameter, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
Parameters:
-
vendors
: List<String> - A list of of vendor IDs to accept -
completion
: A closure called when the operation completes
Returns: None
Example:
cmpManager.acceptVendors(listOf("s2790", "s2791")) { result ->
result.onSuccess {
toastMessage = "Vendors Enabled"
}.onFailure { error ->
toastMessage = "Error: ${error.message}"
}
}
importCMPInfo(cmpString: String)
Imports consent information from a CMP string. This will receive a plain string containing the consent data, usually obtained through the exportCMPInfo
method. This information is persisted in the SharedPreferences area of the device, and at the same time is sent to our backend via a message injected in the WebView, consuming one pageview in the process.
Parameters:
-
cmpString
: String - The CMP string to import -
completion
: A closure called when the operation completes, returning either a failure or success
Returns: None
Example:
val cmpString = "Q1FERkg3QVFERkg3QUFmR01CSVRCQkVnQUFBQUFBQUFBQWlnQUFBQUFBQUEjXzUxXzUyXzUzXzU0XzU1XzU2XyNfczI3ODlfczI3OTBfczI3OTFfczI2OTdfczk3MV9VXyMxLS0tIw"
cmpManager.importCMPInfo(cmpString) { result ->
result.onSuccess {
toastMessage = "Vendors Enabled"
}.onFailure { error ->
toastMessage = "Error: ${error.message}"
}
}
rejectAll(completion: (Result<Unit>) -> Unit)
Rejects consent for all purposes and vendors, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WebView, which will trigger the rejection of all the consents, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
Parameters:
-
completion
: A closure called when the operation completes
Returns: None
Example:
cmpManager.rejectAll { result ->
result.onSuccess {
toastMessage = "All consents rejected"
}.onFailure { error ->
toastMessage = "Error: ${error.message}"
}
}
rejectPurposes(purposes: List<String>, updateVendor: Boolean, completion: (Result<Unit>) -> Unit)
Rejects consent for specified purposes, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WebView, which will trigger the rejection of the determined purposes passed as a parameter, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
Parameters:
-
purposes
:List<String>
- A list of purpose IDs to reject -
updateVendor
:Boolean
- Whether to update related vendors -
completion
: A closure called when the operation completes
Returns: None
Example:
cmpManager.rejectPurposes(listOf("c52", "c53"), true) { result ->
result.onSuccess {
toastMessage = "Purposes disabled"
}.onFailure { error ->
toastMessage = "Error: ${error.message}"
}
}
rejectVendors(vendors: List<String>, completion: (Result<Unit>) -> Unit)
Rejects consent for specified vendors, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WebView, which will trigger the rejection of the determined vendors passed as a parameter, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
Parameters:
-
vendors
: List<String> - A list of vendor IDs to reject -
completion
: A closure called when the operation completes, returning either a failure or a success
Returns: None
Example:
cmpManager.rejectVendors(listOf("s2790", "s2791")) { result ->
result.onSuccess {
toastMessage = "Vendors Disabled"
}.onFailure { error ->
toastMessage = "Error: ${error.message}"
}
}
resetConsentManagementData()
Resets all consent management data. This completely erases al the SharedPreferences area entries related to consents accepted or rejected by the user. It is similar to completely removing the app from the device.
Parameters:
None
Returns: None
Example:
cmpManager.resetConsentManagementData()
CMPManagerDelegate events
didReceiveConsent(consent: String, jsonObject: [String: Any])
This is triggered when the consent layer was closed after the user updating his/her consents OR when invoking methods that cause changes in the consents, like acceptAll, rejectAll, acceptVendors, rejectVendors, etc. It means that the user accepted of rejected some of all of the consents, and that those were correctly saved in the device.
didShowConsentLayer
This is triggered when the consent layer was actually displayed. It means that there was not a consent valid in the device, so a new one should be collected.
didCloseConsentLayer
This is triggered when the SDK checked the need for a consent, but it was not needed and the layer was not displayed. It means that there is already a valid in the device, so a new one is not necessary and tue consent layer will not be displayed.
didReceiveError
This is triggered when the SDK faced any error, returning its code.