PubConsent

Android Integration

28min

The PubConsent SDK is the integration of our PubConsent CMP into Android applications.

The strength of our integration is that it does not require many updates since any changes made within the configurator will be automatically reflected within the app (excluding some specific configurations that need to be done programmatically). Minimum SDK version: 21

Every time significant changes are introduced, you will be contacted via email at the technical address you have configured in our dashboard, allowing you to update the SDK version to the latest one we have published.

For more API or more info of how it works needed don't hesitate to contact our customer support. Every bug or problems during the integration will be a great feedback for us, please don't hesitate to contact us, your satisfaction is our great reward.

Setup

To use the PubConsent SDK in your Android project, add the following dependency to your build.gradle: Our CMP SDK is available on Maven Central here.

Text


Permissions

This SDK requires the following permissions, please ensure to add them to yourAndroidManifest.xml:

Text


How it works

Once the SDK is integrated into an app, it will automatically prompt and display the user interface if necessary. The SDK will retrieve information from our server to prepare and provide your CMP, which you have configured through our dashboard. When the app wants to process personal data, it should "ask" the SDK if consent has been given for the specific purpose and provider.

Kotlin Example

Kotlin


Java Example

Java


Consent sharing with WebView

If you need to reuse the consent collected through our SDK in a WebView pointing to your website, you can refer to the documentation Consent sharing with WebView .

Layout configuration

For configuring the layout you can use the CmpUIConfig class with different styling options. This class offers also some preset layouts like

  • configureHalfScreenBottom
  • configureHalfScreenTop
  • configureCenterScreen
  • configureSmallCenterScreen
  • configureLargeTopScreen
  • configureLargeBottomScreen

The default UI type will be the one that use the Popup Window, you can change the UI Type by setting the UIConfig parameter:

  • CmpUIConfig.uiType = CmpUIType.DIALOG
  • CmpUIConfig.uiType = CmpUIType.POPUP
  • CmpUIConfig.uiType = CmpUIType.FRAGMENT
  • CmpUIConfig.uiType = CmpUIType.ACTIVITY

Two important parameter will determine the Popup and Dialog behaviour:

Popup behaviour

Parameter

isFocusable = true

isFocusable = false

isOutsideTouchable = true

Dismisses on outside touch. Can gain focus for input events.

Dismisses on outside touch. Does not gain focus or intercept keyboard input.

isOutsideTouchable = false

Does not dismiss on outside touch. Can gain focus and intercept input events.

Does not dismiss on outside touch. Does not gain focus or intercept keyboard input.

Dialog behaviour

Parameter

isFocusable = true

isFocusable = false

isOutsideTouchable = true

Dismisses on outside touch (setCanceledOnTouchOutside(true)). Dialog is focusable by default.

Dialog does not dismiss on outside touch and might not behave as expected since dialogs are typically focusable.

isOutsideTouchable = false

Does not dismiss on outside touch (setCanceledOnTouchOutside(false)). Dialog remains focusable and can intercept input events.

Dialog does not dismiss on outside touch and might not behave as expected due to the lack of focusability.

Example of FragmentLayout:

R.id.cmpContainer is a Framelayout which could look like this in the activity layout xml in layout/{your_activity}.xml

Preventing the Back Action When the UI is Displayed

To prevent the closure of the UI, you need to override the onBackPressed function and check whether the CMP (Consent Management Platform) is being displayed. To verify that the CMP UI is shown, you can rely on the previously shown callbacks.

Kotlin


SDK APIs

Cmp class

Constructors

  1. Cmp(Context context)
    • Description: Initializes the CMP with the specified context and the ID of the content view.
    • Parameters:
      • context: The context of the application/activity.

Public Methods

  1. run(CmpConfig cmpConfig)
    • Description: Runs the CMP with the provided configuration.
    • Parameters:
      • cmpConfig: The configuration object for the CMP.
    • Usage: This method initializes and starts the CMP with the specified configuration.
  2. showNotice(Activity activity)
    • Description: Displays the CMP window to ask for user consent.
    • Parameters:
      • activity: The activity context where the CMP window will be displayed.
    • Usage: Call this method to prompt the user for consent using the CMP window.

CmpConfig Class

Constructors

  1. CmpConfig(id: String, appName: String, callbacks: CmpCallbacks, debug: Boolean, retryConfig: RetryConfig)
    • Description: Constructs a configuration object for the CMP.
    • Parameters:
      • id: The ID of the CMP.
      • appName: The name of the application.
      • callbacks: Callback functions for CMP events.
      • debug: For debugging purpose.
      • retryConfig: For configuring the parameters available by the retry mechanism.

RetryConfig Class

Constructors

  1. RetryConfig(maxRetries: Int, retryDelay: Int, retriesBackOffFactor: Double, timeout: Long)
    • Description: Constructs a configuration object for the retry mechanism that fetch the CMP.
    • Parameters:
      • maxRetries: The maximum number of retry attempts. Default is 3.
      • retryDelay: The delay (in milliseconds) between consecutive retry attempts. Default is 200.
      • retriesBackOffFactor: The factor by which the retry delay should be multiplied for exponential backoff. Default is 1.3.
      • timeout: The maximum duration (in milliseconds) within which the retry attempts must complete. Default is 7500.

CmpCallbacks Interface

Methods

  1. onConsentReadyCallback()
    • Description: Callback function invoked when consent is ready.
    • Note: Below, you will find the documentation on the available implementations of the ConsentApiInterface.
  2. onCmpUIClosedCallback()
    • Description: Callback function invoked when the CMP window is closed.
  3. onCmpUIOpenCallback()
    • Description: Callback function invoked when the CMP window is opened.
  4. onErrorCallback(error: String)
    • Description: Callback function invoked when an error occurs.
    • Parameters:
      • error: Description of the error.
  5. onGoogleConsentModeCallback(consentMap: Map<Int, Int>)
    • Description: Callback function invoked when the google consent mapping is ready.
    • Parameters:
      • consentMap: Map containing consent mappings.

PubConsent ConsentApiInterface

Using the API exposed through onConsentReadyCallback, you can check which of the following implementations are available using the getCmpType() method (this choice depends on the configuration saved through the PubConsent configurator).

Below, we present the APIs for each CMP Type we offer. The following methods are accessible via the instance consentApiInstance: any PubConsent.ConsentApiInterface, available as a parameter in the callback: onConsentReadyCallback.



API for CmpType.TCF_V2_GDPR

When consent is related to the TCF CMP (i.e., consentApiInterface.getCmpType() == CmpType.TCF_V2_GDPR), the following APIs are available:

isVendorConsentEnabled(vendorId: Int): Boolean

Description: Checks if consent is enabled for a specific vendor. Parameters:

  • vendorId: The vendor ID to check consent for.

Returns: true if consent is granted for the specified vendor, otherwise false. Usage: Use this method to determine if consent has been granted for a specific vendor.



isPurposeConsentEnabled(purposeId: Int): Boolean

Description: Checks if consent is enabled for a specific purpose. Parameters:

  • purposeId: The purpose ID to check consent for.

Returns: true if consent is granted for the specified purpose, otherwise false. Usage: Use this method to determine if consent has been granted for a specific purpose.



isFeatureCookiesEnabled(): Boolean

Description: Checks if consent is enabled for feature cookies. Returns: true if consent is granted for feature cookies, otherwise false. Usage: Use this method to determine if consent has been granted for feature cookies.



isUserExperienceCookiesEnabled(): Boolean

Description: Checks if consent is enabled for user experience cookies. Returns: true if consent is granted for user experience cookies, otherwise false. Usage: Use this method to determine if consent has been granted for user experience cookies.



isMeasurementCookiesEnabled(): Boolean

Description: Checks if consent is enabled for measurement cookies. Returns: true if consent is granted for measurement cookies, otherwise false. Usage: Use this method to determine if consent has been granted for measurement cookies.



getGoogleConsentMode() : Map<GoogleConsentModeType, GoogleConsentModeStatus>?

Description: Retrieves the status of Google Consent Mode for the supported consent types. Returns: A map containing consent types (GoogleConsentModeType) and their respective statuses (GoogleConsentModeStatus). Returns null if no configuration is available. Usage: Use this method to obtain the consent status related to Google Consent Mode and determine whether consent has been granted, denied, or is unknown for each consent type.



API for CmpType.GOOGLE_CONSENT_MODE

When consent is related to the CMP based only on Google Consent Mode (i.e., consentApiInterface.getCmpType() == CmpType.GOOGLE_CONSENT_MODE), the following API is available:

getGoogleConsentMode() : Map<GoogleConsentModeType, GoogleConsentModeStatus>?

Description: Retrieves the status of Google Consent Mode for the supported consent types. Returns: A map containing consent types (GoogleConsentModeType) and their respective statuses (GoogleConsentModeStatus). Returns null if no configuration is available. Usage: Use this method to obtain the consent status related to Google Consent Mode and determine whether consent has been granted, denied, or is unknown for each consent type.

Troubleshooting

ProGuard bugs

ProGuard può talvolta offuscare i nomi delle classi o rimuovere i metodi che vengono richiamati dinamicamente tramite reflection. Per risolvere questo problema, è necessario specificare le classi e i metodi che devono rimanere intatti nel file di configurazione di ProGuard utilizzando la direttiva -keep.