Skip to main content

Examples


Full description of the integration example below

In this example, we'll make sure that the CMP is displayed to users when the application is launched (startup of our activity).

Here are the various steps in this example at first launch

  1. Check SDK if already initialized (return false)
  2. Initialize the CMP by calling AppConsentSDK#initialize if not.
  3. Once the SDK has been started, record callbacks to let you know when the user has finished entering information.
  4. Call the #tryToDisplayNotice(false) method, which displays the CMP only when necessary. (in the example, it will be displayed).
  5. check the return of #tryToDisplayNotice(false) to determine whether the CMP has been displayed or not (return to true).
  6. Once the user has given their consent, the #presentConsentGiven method will be called and you can, for example, start up your PUB SDK (AdMob, Vungle, Amazon mobile Ads, etc.).

Here are the various steps in this example on the next launch (having killed the application)

  1. Check SDK if it has already been initialized (return false)
  2. Initialize the CMP by calling AppConsentSDK#initialize, if not.
  3. Once the SDK has been started, record callbacks to let you know when the user has finished entering information.
  4. Call the #tryToDisplayNotice(false) method, which displays the CMP only if necessary.
  5. check the return of #tryToDisplayNotice(false) to determine whether or not the CMP has been displayed (result set to false).
  6. (By your own) Try to call the #checkForUpdate method to check whether the Notice has been updated from your Dashboard (unless you take action, result is false).
  7. No action is required, so you can start up your PUB SDK (AdMob, Vungle, Amazon mobile Ads, etc.).

Here are the various steps in this example on the next launch (quitting the application and bringing it forward again without killing it)

  1. Check SDK if it has already been initialized (return to true)
  2. Recover instance for local use in our activity
  3. Call the #tryToDisplayNotice(false) method, which displays the CMP only if necessary.
  4. check the return of #tryToDisplayNotice(false) to determine whether the CMP has been displayed or not (result set to false).
  5. (By your own) Try to call the #checkForUpdate method to check whether the Notice has been updated from your Dashboard (unless you take action, result is false).
  6. No action is required, so you can start up your PUB SDK (AdMob, Vungle, Amazon mobile Ads, etc.).

Here are the various steps in this example at the next launch AND after you've modified your Notice from the Dashboard (by quitting the application and bringing it forward again without killing it)

  1. Check SDK if it has already been initialized (return to true)
  2. Recover instance for local use in our activity
  3. Call the #tryToDisplayNotice(false) method, which displays the CMP only if necessary.
  4. check the return of #tryToDisplayNotice(false) to determine whether the CMP has been displayed or not (result set to false).
  5. (By your own) Try to call the #checkForUpdate method to check whether the Notice has been updated from your Dashboard (result is true because your Notice has been updated).
  6. Local deletion of previous user consent #clearConsent
  7. Call the #tryToDisplayNotice(false) method to display the CMP only if necessary. (return to true)
  8. Once the user has given consent, the #presentConsentGiven method will be called and you can, for example, start your PUB SDK (AdMob, Vungle, Amazon mobile Ads, etc.).
import com.sfbx.appconsent.core.model.gcm.GCMStatus
import com.sfbx.appconsentv3.AppConsent
import com.sfbx.appconsentv3.ui.AppConsentSDK
import com.sfbx.appconsentv3.ui.listener.OnPresentNoticeListener
import com.sfbx.appconsentv3.ui.model.ACConfiguration

class MainActivity : AppCompatActivity() {

private var appConsent: AppConsent? = null
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(binding.root)

binding.buttonDisplaySecondActivity.setOnClickListener {
startActivity(Intent(this, SecondActivity::class.java))
}

/*
Shows whether the SDK has already been initialized
*/
if (!AppConsentSDK.isSdkInitialized() || AppConsentSDK.getInstance() == null) {
initCmpModule()
} else {
if (appConsent == null) {
/*
Retrieves the AppConsent instance or null if it has not yet been instantiated, for example
*/
appConsent = AppConsentSDK.getInstance()
}
// To be safe
appConsent?.let { appConsentInstance ->
tryToDisplayCmpIfNeeded(appConsentInstance)
}
}
}

/**
* This method is not included in this example, as it doesn't really need to be used.
*
* The SDK initialization itself performs the startup check if its last initialization was more than 30 minutes ago.
*
* The only conditions for using this method would be if:
* - you have configured your Notice by selecting this option: When saving changes to the notice, display the notice to all visitors.
* - you plan to update your Notice often (more often than the : Consent retention period configurable in your Notice)
* - Your users rarely restart your application (and therefore do not go through the initialization check above).
*
* In which case, using this method from time to time may be a solution.
*
* But we encourage you to let the SDK handle this part on its own.
*/
private fun checkIfNoticeHasBeenUpdated(appConsent: AppConsent) {
appConsent.checkForUpdate({ isNeedToPresentTheCmp: Boolean ->
// Your Notice has been updated, you must represent the CMP to your users
if (true == isNeedToPresentTheCmp) {
/*
Deletes old user consent locally.
This step is not mandatory, but it avoids the need to make another network call
to check whether the Notice has been updated,
as no consent will be present on the user's device.
*/
appConsent.clearConsent()
tryToDisplayCMP(appConsent)
} else {
/*
The Notice is the same as when it was last checked
(you have made no changes since the board or
it has not been updated internally by us, e.g. by updating a vendor)
*/
}
}) { _: Throwable? ->
/*
An error has occurred
*/
}
}

/**
* Try to display the CMP
*
* @return true, if the CMP displaying, false otherwise
*/
private fun tryToDisplayCMP(appConsent: AppConsent): Boolean {
/*
Try to display the CMP according to certain rules.
*/
return appConsent.tryToDisplayNotice(false)
}

private fun tryToDisplayCmpIfNeeded(appConsent: AppConsent) {
/*
Try to display the CMP according to certain rules first.
*/
if (false == tryToDisplayCMP(appConsent)) {
/*
The user has already given consent;
The user is not part of an area subject to the application of GDPR;
etc.
*/
}
}

/*
Initializes the consent management platform module when the activity is created
*/
private fun initCmpModule() {
/*
ACConfiguration is used to configure the CMP.
In this example:
- We set forceApplyGDPR to true to display the CMP regardless of the user's region.
- Decide to display the CMP in FullScreen rather than modal.
- CMP is configured so that layer 1 buttons are displayed vertically and not horizontally (except in landscape mode).
*/
val acConfiguration = ACConfiguration.Builder()
.setForceApplyGDPR(true)
.setFullScreenMode(true)
.setNeedToDisplayValidationButtonsVertically(isNeedToDisplayButtonsAtVertical = true)
.build()

AppConsentSDK.initialize(
appKey = "YOUR_APP_KEY",
configuration = acConfiguration
) { appConsentInitialized ->
/*
To avoid certain problems, use the instance received by the onReady callback
This has been successfully initialized
*/
appConsent = appConsentInitialized
/*
Registers with CMP callback to know when the user
has given consent, or if an error has occurred
*/
appConsentInitialized.setOnPresentNoticeListener(object : OnPresentNoticeListener {
override fun presentConsentError(error: Throwable?) {
/*
An error has occurred
*/
removeCMPCallback(appConsentInitialized)
}

override fun presentConsentGiven() {
/*
The user has given his consent
*/
updateFirebaseConsent(appConsentInitialized)
removeCMPCallback(appConsentInitialized)
}
})
tryToDisplayCmpIfNeeded(appConsentInitialized)
}
}

private fun removeCMPCallback(appConsent: AppConsent) {
/*
Not mandatory, but avoids keeping a local reference to the callback.
Of course, it all depends on how your project is implemented.
For example, with an SOP Activity, a singleton monitored by a flow, etc.
*/
appConsent.setOnPresentNoticeListener(null)
}

private fun updateFirebaseConsent(appConsent: AppConsent) {
// Consent has just been validated by the user
// Recovers current GCM status (following user consent)
val gcmConsentStatus = appConsent.getGCMConsentStatus()

val newAnalyticsStorage =
if (gcmConsentStatus.isAnalyticsStorageGranted) GRANTED else DENIED
val newAdStorage =
if (gcmConsentStatus.isAdStorageGranted) GRANTED else DENIED
val newAdUserData =
if (gcmConsentStatus.isAdUserDataGranted) GRANTED else DENIED
val newAdPersonalization =
if (gcmConsentStatus.isAdPersonalizationGranted) GRANTED else DENIED

/*
* We do not guarantee that Firebase will work with this example.
* Please refer to the official documentation for details of initialization, re-initialization, reboot and other conditions.
*
* This example is only intended to show how to retrieve ConsentStatus and set it to your Firebase instance.
*/
// Update Firebase status with information provided by the SDK
Firebase.analytics.setConsent {
analyticsStorage(newAnalyticsStorage)
adStorage(newAdStorage)
adUserData(newAdUserData)
adPersonalization(newAdPersonalization)
}
}
}