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
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
- Mobile / Tablet
- TV
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.
- Kotlin
- Java
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
...
}
})
}
}
import com.sfbx.appconsentv3.AppConsent;
import com.sfbx.appconsentv3.ui.AppConsentSDK;
import com.sfbx.appconsentv3.ui.listener.OnPresentNoticeListener;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppConsentSDK.initialize(
"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)) {
removeCMPCallback(appConsentInitialized);
}
return Unit.INSTANCE;
}
);
}
private void registerCallback(@NonNull final AppConsent appConsent) {
appConsent.setOnPresentNoticeListener(new OnPresentNoticeListener() {
@Override
public void presentConsentError(@NonNull Throwable error) {
// remove listener
removeCMPCallback(appConsent);
// Load Ads providers
...
}
@Override
public void presentConsentGiven() {
// remove listener
removeCMPCallback(appConsent);
// Load Ads providers
...
}
});
}
private void removeCMPCallback(@NonNull final AppConsent appConsent) {
appConsent.setOnPresentNoticeListener(null);
}
}
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.
- Kotlin
- Java
import com.sfbx.appconsent.AppConsent
import com.sfbx.appconsent.tv.AppConsentSDK
import com.sfbx.appconsent.tv.listener.OnPresentNoticeListener
import com.sfbx.appconsent.tv.model.error.ACError
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: ACError) {
// remove listener
appConsent.setOnPresentNoticeListener(null)
// Load Ads providers
...
}
override fun presentConsentGiven() {
// remove listener
appConsent.setOnPresentNoticeListener(null)
// Load Ads providers
...
}
})
}
}
import com.sfbx.appconsent.AppConsent;
import com.sfbx.appconsent.tv.AppConsentSDK;
import com.sfbx.appconsent.tv.listener.OnPresentNoticeListener;
import com.sfbx.appconsent.tv.model.error.ACError;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppConsentSDK.initialize(
"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)) {
removeCMPCallback(appConsentInitialized);
}
return Unit.INSTANCE;
}
);
}
private void registerCallback(@NonNull final AppConsent appConsent) {
appConsent.setOnPresentNoticeListener(new OnPresentNoticeListener() {
@Override
public void presentConsentError(@NonNull ACError acError) {
// remove listener
removeCMPCallback(appConsent);
// Load Ads providers
...
}
@Override
public void presentConsentGiven() {
// remove listener
removeCMPCallback(appConsent);
// Load Ads providers
...
}
});
}
private void removeCMPCallback(@NonNull final AppConsent appConsent) {
appConsent.setOnPresentNoticeListener(null);
}
}
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.
- Mobile / Tablet
- TV
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.
- Kotlin
- Java
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)
}
}
import com.sfbx.appconsentv3.AppConsent;
import com.sfbx.appconsentv3.ui.AppConsentSDK;
import com.sfbx.appconsentv3.ui.listener.OnPresentNoticeListener;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.button_display_privacy_policy).setOnClickListener(v -> {
final AppConsent appConsent = AppConsentSDK.getInstance();
/*
Display the CMP to allow your users to consult their consent
*/
boolean isCmpDisplayed = false;
if (appConsent != null) {
defineCallback(appConsent);
isCmpDisplayed = appConsent.tryToDisplayNotice(true);
}
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).
*/
if (appConsent != null) {
removeCMPCallback(appConsent);
}
}
});
}
private void defineCallback(@NonNull final AppConsent appconsent) {
Objects.requireNonNull(appconsent).setOnPresentNoticeListener(new OnPresentNoticeListener() {
@Override
public void presentConsentError(@Nullable Throwable throwable) {
/*
An error has occurred
*/
removeCMPCallback(appconsent);
}
@Override
public void presentConsentGiven() {
/*
The user has updated his consent
*/
removeCMPCallback(appconsent);
// Refresh Ads provider
}
});
}
private void removeCMPCallback(@NonNull final AppConsent appconsent) {
appconsent.setOnPresentNoticeListener(null);
}
}
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.
- Kotlin
- Java
import com.sfbx.appconsent.tv.AppConsentSDK
import com.sfbx.appconsent.tv.listener.OnPresentNoticeListener
import com.sfbx.appconsent.tv.model.error.ACError
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
findViewById<View>(R.id.button_display_privacy_policy).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: ACError) {
/*
An error has occurred
*/
removeCMPCallback()
}
override fun presentConsentGiven() {
/*
The user has updated his consent
*/
removeCMPCallback()
// Refresh Ads provider
}
})
/*
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)
}
}
import com.sfbx.appconsent.AppConsent;
import com.sfbx.appconsent.tv.AppConsentSDK;
import com.sfbx.appconsent.tv.listener.OnPresentNoticeListener;
import com.sfbx.appconsent.tv.model.error.ACError;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.button_display_privacy_policy).setOnClickListener(v -> {
final AppConsent appConsent = AppConsentSDK.getInstance();
/*
Display the CMP to allow your users to consult their consent
*/
boolean isCmpDisplayed = false;
if (appConsent != null) {
defineCallback(appConsent);
isCmpDisplayed = appConsent.tryToDisplayNotice(true);
}
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).
*/
if (appConsent != null) {
removeCMPCallback(appConsent);
}
}
});
}
private void defineCallback(@NonNull final AppConsent appconsent) {
Objects.requireNonNull(appconsent).setOnPresentNoticeListener(new OnPresentNoticeListener() {
@Override
public void presentConsentError(@NonNull ACError acError) {
/*
An error has occurred
*/
removeCMPCallback(appconsent);
}
@Override
public void presentConsentGiven() {
/*
The user has updated his consent
*/
removeCMPCallback(appconsent);
// Refresh Ads provider
}
});
}
private void removeCMPCallback(@NonNull final AppConsent appconsent) {
appconsent.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.