Skip to content

Advanced Usage

You can listen to consent events in your activity by adding and removing listeners in the onStart and onStop methods:

override fun onStart() {
super.onStart()
SPConsentEngine.addListener(
MOBILE_CONSENT_EVENT_CODE,
object : SPConsentEventListener {
override fun onConsentAction(data: SPDataMessage<SPConsentEvent>) {
//Custom logic
}
}
)
}
override fun onStop() {
SPConsentEngine.removeListener(YOUR_UNIQUE_EVENT_CODE)
super.onStop()
}

Here, MOBILE_CONSENT_EVENT_CODE is a unique code you can use to register/unregister events and track updates on consent actions.

Important Note: The response from the onConsentAction callback may come from a background thread. Be sure to handle thread switching (e.g., using Handler, runOnUiThread(), or lifecycleScope) when updating the UI, as UI updates must occur on the main thread.

Additional Way to Listen to Consent Events: As an additional way to listen to consent events, you can now observe consent events using the following method:

SPConsentEngine.getConsentEventsData(YOUR_UNIQUE_EVENT_CODE)
.observe(activity) { data ->
//Custom logic
}

Event Codes

When using the SDK to listen to consent events, it’s important to understand how event codes work.

  • Event Codes: Event codes are unique identifiers that is used to register/unregister consent events. The event code helps distinguish between different consent events, particularly when dealing with multiple listeners or consent events across different activities or classes.

  • Automatic Event Code Registration: When you call getConsentEventsData(UNIQUE_EVENT_CODE) with an event code, the SDK automatically registers that event code. You do not need to manually register the event code, as this is handled internally by the SDK. This ensures that the event codes are uniquely associated.

To enable detailed logs for debugging during development, activate the logging flags provided by the SDK:

SPLogger.ERROR_LOGS_ENABLED = true
SPLogger.INFO_LOGS_ENABLED = true
SPLogger.WARNING_LOGS_ENABLED = true

These settings will enable:

  • Error Logs – to capture critical issues.
  • Info Logs – for general SDK activity and flow.
  • Warning Logs – to monitor potential misconfigurations or unexpected behavior.

⚠️ Tip: Disable logging in production to avoid exposing sensitive data.

Each time the app is installed (fresh install) or during a new session, Secure Privacy Mobile Consent SDK generates a unique clientId for the user. This clientId can be retrieved by calling the following method.

val clientId = spConsentEngine.getClientId("YOUR_APPLICATION_ID")

This clientId can be used to associate the user’s consent with internal identifiers within your app or backend systems. You can also use this clientId for custom user identification and to ensure accurate tracking of consent preferences.

You can get the country code detected by the SDK using:

val countryCode = spConsentEngine?.getLocale("YOUR_APPLICATION_ID")

It returns a country code like US-CA or IN based on the user’s region.

To clear the session, call clearSession(). This method is useful when you want to ensure that all local session-specific sdk data is cleared. It is typically used when a user logs out or resets their preferences.

spConsentEngine.clearSession()