PubConsent

Android Integration

17min

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


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. askConsent(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.
  3. isVendorConsentEnabled(vendorId: Int): Boolean
    • Description: Checks if consent is enabled for a specific vendor.
    • Parameters:
      • vendorId: The ID of the vendor to check consent for.
    • Returns: true if consent is enabled for the specified vendor, otherwise.
    • Usage: Use this method to determine if consent is granted for a particular vendor.
  4. isPurposeConsentEnabled(purposeId: Int): Boolean
    • Description: Checks if consent is enabled for a specific vendor.
    • Parameters:
      • purposeId: The ID of the purpose to check consent for.
    • Returns: true if consent is enabled for the specified purpose, otherwise.
    • Usage: Use this method to determine if consent is granted for a particular purpose.
  5. isFeatureCookiesEnabled(): Boolean
    • Description: Checks if consent is enabled for feature cookies.
    • Returns: true if consent is enabled for feature cookies, otherwise.
    • Usage: Use this method to determine if consent is granted for feature cookies.
  6. isUserExperienceCookiesEnabled(): Boolean
    • Description: Checks if consent is enabled for user experience cookies.
    • Returns: true if consent is enabled for user experience cookies, otherwise.
    • Usage: Use this method to determine if consent is granted for user experience cookies.
  7. isMeasurementCookiesEnabled(): Boolean
    • Description: Checks if consent is enabled for measurement cookies.
    • Returns: true if consent is enabled for measurement cookies, otherwise.
    • Usage: Use this method to determine if consent is granted for measurement cookies.

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.
  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.

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.