Skip to main content

Integration


How to use AppConsent

1. Get your AppKey

The first step is to create your source / notice and retrieve your generated YOUR_APP_KEY from AppConsent : https://app.appconsent.io

GetAppkey

2. Initialize AppConsent & use it

The second step is to initialize the SDK and wait for it to be started before using it.

tip

We recommend you do this in the application class, splashscreen, or your "android.intent.category.LAUNCHER" activity.

The idea is to be able to display the CMP as soon as possible, so that you can load your advertising supplier.

note

Even if your ad provider registers with the user consent update events and this is transparent to you, some providers may not work correctly with this solution. For this reason, we recommend that you initialize your provider's SDK only when consent has been given, to ensure that your ads are in accordance with user consent. Think to update it each time the user updates his consent too, thank to callbacks

info

In the example below, the code focuses on using the SDK exclusively.
The potential imports and visual elements in these examples are therefore not present, as they are strongly linked to the Android structure and project, which are independent of the SDK.

import com.sfbx.appconsentv3.AppConsent
import com.sfbx.appconsentv3.ui.AppConsentSDK
import com.sfbx.appconsentv3.ui.listener.OnPresentNoticeListener

class MainActivity : AppCompatActivity() {

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

AppConsentSDK.initialize(
appKey = "YOUR_APP_KEY"
) { appConsentInitialized ->

// Set a callback to let you know when the user has completed validation
registerCallback(appConsentInitialized)

// Once loaded, try to display CMP if needed
if(false == appConsentInitialized.tryToDisplayNotice(false)) {
appConsentInitialized.setOnPresentNoticeListener(null)
}
}
}

private fun registerCallback(appConsent: AppConsent){
appConsent.setOnPresentNoticeListener(object : OnPresentNoticeListener {

override fun presentConsentError(error: Throwable?) {
// remove listener
appConsent.setOnPresentNoticeListener(null)

// Load Ads providers
...
}

override fun presentConsentGiven() {
// remove listener
appConsent.setOnPresentNoticeListener(null)

// Load Ads providers
...
}
})
}
}

3. Suggest CMP to your users

It is also advisable to provide your users with an entry allowing them to view and modify their consent, such as a configuration screen.

info

In the example below, the code focuses on using the SDK exclusively.
The potential imports and visual elements in these examples are therefore not present, as they are strongly linked to the Android structure and project, which are independent of the SDK.

import com.sfbx.appconsentv3.ui.AppConsentSDK
import com.sfbx.appconsentv3.ui.listener.OnPresentNoticeListener

class SecondActivity : AppCompatActivity() {

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

binding.buttonDisplayPrivacyPolicy.setOnClickListener {
/*
Registers with CMP callback to know when the user
has updated consent, or if an error has occurred
*/
AppConsentSDK.getInstance()
?.setOnPresentNoticeListener(object : OnPresentNoticeListener {
override fun presentConsentError(error: Throwable?) {
// remove listener
removeCMPCallback()
}

override fun presentConsentGiven() {
// remove listener
removeCMPCallback()

// Refresh Ads provider
}
})

// Try to display the CMP to allow your users to consult their consent
val isCmpDisplayed =
AppConsentSDK.getInstance()?.tryToDisplayNotice(force = true) ?: false
if (false == isCmpDisplayed) {
/*
The CMP is not initialized (getInstance() return null),
make sure it is initialized (see 2. Create the AppConsent instance).
Also check that your activity has not been started in a new process
(new process, new memory stack, uninitialized singleton).
*/
removeCMPCallback()
}
}
}

private fun removeCMPCallback() {
/*
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.
*/
AppConsentSDK.getInstance()?.setOnPresentNoticeListener(null)
}
}
note

Would you like a more complete example?
We suggest you read the Go further page and take a look at the technical documentation.