Examples
- Mobile / Tablet
- TV
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
- Check SDK if already initialized (return false)
- Initialize the CMP by calling AppConsentSDK#initialize if not.
- Once the SDK has been started, record callbacks to let you know when the user has finished entering information.
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method, which displays the CMP only when necessary. (in the example, it will be displayed).
- check the return of #tryToDisplayNoticeFromUiContext(uiContext, false) to determine whether the CMP has been displayed or not (return to true).
- 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)
- Check SDK if it has already been initialized (return false)
- Initialize the CMP by calling AppConsentSDK#initialize, if not.
- Once the SDK has been started, record callbacks to let you know when the user has finished entering information.
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method, which displays the CMP only if necessary.
- check the return of #tryToDisplayNoticeFromUiContext(uiContext, false) to determine whether or not the CMP has been displayed (result set to false).
- (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).
- 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)
- Check SDK if it has already been initialized (return to true)
- Recover instance for local use in our activity
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method, which displays the CMP only if necessary.
- check the return of #tryToDisplayNoticeFromUiContext(uiContext, false) to determine whether the CMP has been displayed or not (result set to false).
- (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).
- 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)
- Check SDK if it has already been initialized (return to true)
- Recover instance for local use in our activity
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method, which displays the CMP only if necessary.
- check the return of #tryToDisplayNoticeFromUiContext(uiContext, false) to determine whether the CMP has been displayed or not (result set to false).
- (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).
- Local deletion of previous user consent #clearConsent
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method to display the CMP only if necessary. (return to true)
- 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.).
- Kotlin
- Java
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)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
/*
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()
}
tryToDisplayCmpAndCheckUpdateIfNeeded()
}
}
/**
* 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.
*
* 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?.checkForUpdate(
{ isNeedToPresentTheCmp ->
// 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()
/*
Remember to re-register for callbacks if you have previously removed them,
otherwise you will not know when the user has given their consent.
*/
registerCallbacks()
if (false == tryToDisplayCMP()) {
/*
At this stage, if the CMP is not displayed, it is possible, for example:
that the user is no longer in a geographical area subject to GDPR
*/
removeCMPCallback()
}
} 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)
*/
}
},
{ _ ->
/*
An error has occurred
*/
})
}
/**
* Try to display the CMP
*
* @return true, if the CMP displaying, false otherwise
*/
private fun tryToDisplayCMP(): Boolean {
/*
Try to display the CMP according to certain rules.
*/
return appConsent?.tryToDisplayNoticeFromUiContext(uiContext = this@MainActivity, false) == true
}
private fun tryToDisplayCmpAndCheckUpdateIfNeeded() {
/*
Try to display the CMP according to certain rules first.
*/
if (false == tryToDisplayCMP()) {
/*
The user has already given consent;
The user is not part of an area subject to the application of GDPR;
etc.
*/
removeCMPCallback()
/*
The Notice has not been displayed,
so we'll have to check whether it has been updated
and whether it needs to be shown to users again.
*/
checkIfNoticeHasBeenUpdated()
}
}
private fun registerCallbacks() {
/*
Registers with CMP callback to know when the user
has given consent, or if an error has occurred
*/
appConsent?.setOnPresentNoticeListener(object : OnPresentNoticeListener {
override fun presentConsentError(error: Throwable?) {
/*
An error has occurred
*/
removeCMPCallback()
}
override fun presentConsentGiven() {
/*
The user has given his consent
*/
appConsent?.let { appConsentNN ->
updateFirebaseConsent(appConsentNN)
}
removeCMPCallback()
}
})
}
/*
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).
- We configure CMP so that hypertext links are no longer displayed in a webview and/or the user is redirected outside the application if the requested link is a file, for example; instead, a popup presenting a qr code will be presented to your users (mostly useful on Automotive / Tablet)
*/
val acConfiguration = ACConfiguration.Builder()
.setForceApplyGDPR(true)
.setFullScreenMode(true)
.setNeedToDisplayValidationButtonsVertically(isNeedToDisplayButtonsAtVertical = true)
.setNeedToReplaceUrlViewerByQrCode(isNeedToReplaceUrlViewerByQrCode = true)
.build()
AppConsentSDK.initialize(
appKey = "YOUR_APPKEY",
configuration = acConfiguration
) { appConsentInitialized ->
/*
To avoid certain problems, use the instance received by the onReady callback
This has been successfully initialized
*/
appConsent = appConsentInitialized
registerCallbacks()
tryToDisplayCmpAndCheckUpdateIfNeeded()
}
}
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.
*/
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
}
}
}
import com.sfbx.appconsent.core.model.gcm.GCMStatus;
import com.sfbx.appconsentv3.AppConsent;
import com.sfbx.appconsentv3.ui.AppConsentSDK;
import com.sfbx.appconsentv3.ui.AppConsentTheme;
import com.sfbx.appconsentv3.ui.listener.OnPresentNoticeListener;
import com.sfbx.appconsentv3.ui.model.ACConfiguration;
public class MainActivity extends AppCompatActivity {
private AppConsent appConsent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
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();
}
tryToDisplayCmpAndCheckUpdateIfNeeded();
}
}
/**
* 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.
*
* 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 void checkIfNoticeHasBeenUpdated() {
appConsent.checkForUpdate(
isNeedToPresentTheCmp -> {
// 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();
/*
Remember to re-register for callbacks if you have previously removed them,
otherwise you will not know when the user has given their consent.
*/
registerCallbacks();
if (false == tryToDisplayCMP()) {
/*
At this stage, if the CMP is not displayed, it is possible, for example:
that the user is no longer in a geographical area subject to GDPR
*/
removeCMPCallback();
}
return Unit.INSTANCE;
} 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)
*/
}
return Unit.INSTANCE;
}, onError -> {
/*
An error has occurred
*/
return Unit.INSTANCE;
});
}
/**
* Try to display the CMP
*
* @return true, if the CMP displaying, false otherwise
*/
private Boolean tryToDisplayCMP() {
/*
Try to display the CMP according to certain rules.
*/
return (appConsent.tryToDisplayNoticeFromUiContext(this, false) == true);
}
private void tryToDisplayCmpAndCheckUpdateIfNeeded() {
/*
Try to display the CMP according to certain rules first.
*/
if (false == tryToDisplayCMP()) {
/*
The user has already given consent;
The user is not part of an area subject to the application of GDPR;
etc.
*/
removeCMPCallback();
/*
The Notice has not been displayed,
so we'll have to check whether it has been updated
and whether it needs to be shown to users again.
*/
checkIfNoticeHasBeenUpdated();
}
}
private void registerCallbacks() {
/*
Registers with CMP callback to know when the user
has given consent, or if an error has occurred
*/
appConsent.setOnPresentNoticeListener(new OnPresentNoticeListener() {
@Override
public void presentConsentError(@Nullable Throwable throwable) {
/*
An error has occurred
*/
removeCMPCallback();
}
@Override
public void presentConsentGiven() {
/*
The user has given his consent
*/
if (appConsent != null) {
updateFirebaseConsent(appConsent);
}
removeCMPCallback();
}
});
}
/*
Initializes the consent management platform module when the activity is created
*/
private void 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).
- We configure CMP so that hypertext links are no longer displayed in a webview and/or the user is redirected outside the application if the requested link is a file, for example; instead, a popup presenting a qr code will be presented to your users (mostly useful on Automotive / Tablet)
*/
final ACConfiguration acConfiguration = new ACConfiguration.Builder()
.setForceApplyGDPR(true)
.setFullScreenMode(true)
.setNeedToDisplayValidationButtonsVertically(true)
.setNeedToReplaceUrlViewerByQrCode(true)
.defineAppConsentTheme(new AppConsentTheme.Builder(this).iconDrawable(null).build())
.build();
AppConsentSDK.initialize(
"YOUR_APPKEY",
acConfiguration,
appConsentInitialized -> {
/*
To avoid certain problems, use the instance received by the onReady callback
This has been successfully initialized
*/
appConsent = appConsentInitialized;
registerCallbacks();
tryToDisplayCmpAndCheckUpdateIfNeeded();
return Unit.INSTANCE;
});
}
private void 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.
*/
if (appConsent != null) {
appConsent.setOnPresentNoticeListener(null);
}
}
private void updateFirebaseConsent(AppConsent appConsent) {
// Consent has just been validated by the user.
// Recovers current GCM status (following user consent).
final GCMStatus gcmConsentStatus = appConsent.getGCMConsentStatus();
final FirebaseAnalytics.ConsentStatus newAnalyticsStorage;
if (gcmConsentStatus.isAnalyticsStorageGranted()) newAnalyticsStorage = GRANTED;
else newAnalyticsStorage = DENIED;
final FirebaseAnalytics.ConsentStatus newAdStorage;
if (gcmConsentStatus.isAdStorageGranted()) newAdStorage = GRANTED;
else newAdStorage = DENIED;
final FirebaseAnalytics.ConsentStatus newAdUserData;
if (gcmConsentStatus.isAdUserDataGranted()) newAdUserData = GRANTED;
else newAdUserData = DENIED;
final FirebaseAnalytics.ConsentStatus newAdPersonalization;
if (gcmConsentStatus.isAdPersonalizationGranted()) newAdPersonalization = GRANTED;
else newAdPersonalization = 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
final Map<FirebaseAnalytics.ConsentType, FirebaseAnalytics.ConsentStatus> consentStatus = new HashMap<>();
consentStatus.put(FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE, newAnalyticsStorage);
consentStatus.put(FirebaseAnalytics.ConsentType.AD_STORAGE, newAdStorage);
consentStatus.put(FirebaseAnalytics.ConsentType.AD_USER_DATA, newAdUserData);
consentStatus.put(FirebaseAnalytics.ConsentType.AD_PERSONALIZATION, newAdPersonalization);
FirebaseAnalytics.getInstance(getApplicationContext()).setConsent(consentStatus);
}
}
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
- Check SDK if already initialized (return false)
- Initialize the CMP by calling AppConsentSDK#initialize if not.
- Once the SDK has been started, record callbacks to let you know when the user has finished entering information.
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method, which displays the CMP only when necessary. (in the example, it will be displayed).
- check the return of #tryToDisplayNoticeFromUiContext(uiContext, false) to determine whether the CMP has been displayed or not (return to true).
- 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)
- Check SDK if it has already been initialized (return false)
- Initialize the CMP by calling AppConsentSDK#initialize, if not.
- Once the SDK has been started, record callbacks to let you know when the user has finished entering information.
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method, which displays the CMP only if necessary.
- check the return of #tryToDisplayNoticeFromUiContext(uiContext, false) to determine whether or not the CMP has been displayed (result set to false).
- (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).
- 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)
- Check SDK if it has already been initialized (return to true)
- Recover instance for local use in our activity
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method, which displays the CMP only if necessary.
- check the return of #tryToDisplayNoticeFromUiContext(uiContext, false) to determine whether the CMP has been displayed or not (result set to false).
- (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).
- 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)
- Check SDK if it has already been initialized (return to true)
- Recover instance for local use in our activity
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method, which displays the CMP only if necessary.
- check the return of #tryToDisplayNoticeFromUiContext(uiContext, false) to determine whether the CMP has been displayed or not (result set to false).
- (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).
- Local deletion of previous user consent #clearConsent
- Call the #tryToDisplayNoticeFromUiContext(uiContext, false) method to display the CMP only if necessary. (return to true)
- 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.).
- Kotlin
- Java
import com.sfbx.appconsent.AppConsent
import com.sfbx.appconsent.tv.AppConsentSDK
import com.sfbx.appconsent.tv.listener.AppConsentErrorListener
import com.sfbx.appconsent.tv.listener.AppConsentLogListener
import com.sfbx.appconsent.tv.listener.AppConsentNavigationListener
import com.sfbx.appconsent.tv.listener.OnPresentNoticeListener
import com.sfbx.appconsent.tv.model.ACConfiguration
import com.sfbx.appconsent.tv.model.AppConsentTVError
import com.sfbx.appconsent.tv.model.AppConsentTVLog
import com.sfbx.appconsent.tv.model.error.ACError
class MainActivity : AppCompatActivity() {
private var appConsent: AppConsent? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*
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()
}
tryToDisplayCmpAndCheckUpdateIfNeeded()
}
}
/**
* 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.
*
* 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?.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()
/*
Remember to re-register for callbacks if you have previously removed them,
otherwise you will not know when the user has given their consent.
*/
registerCallbacks()
if (false == tryToDisplayCMP()) {
/*
At this stage, if the CMP is not displayed, it is possible, for example:
that the user is no longer in a geographical area subject to GDPR
*/
removeCMPCallback()
}
} 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)
*/
removeCMPCallback()
}
}) { _: Throwable? ->
/*
An error has occurred
*/
removeCMPCallback()
}
}
/**
* Try to display the CMP
*
* @return true, if the CMP displaying, false otherwise
*/
private fun tryToDisplayCMP(): Boolean {
/*
Try to display the CMP according to certain rules.
*/
return appConsent?.tryToDisplayNotice(false) == true
}
private fun tryToDisplayCmpAndCheckUpdateIfNeeded() {
/*
Try to display the CMP according to certain rules first.
*/
if (false == tryToDisplayCMP()) {
/*
The user has already given consent;
The user is not part of an area subject to the application of GDPR;
etc.
*/
/*
The Notice has not been displayed,
so we'll have to check whether it has been updated
and whether it needs to be shown to users again.
*/
checkIfNoticeHasBeenUpdated()
}
}
/*
Initializes the consent management platform module when the activity is created
*/
private fun initCmpModule() {
/*
Add an error listener logger
*/
AppConsentSDK.setErrorLogListener(object : AppConsentErrorListener {
override fun onError(error: AppConsentTVError) {
Log.e(
MainActivity::class.java.simpleName,
error.toString()
)
}
})
/*
Add an information listener logger
*/
AppConsentSDK.setInformationLogListener(object : AppConsentLogListener {
override fun onLogReceived(log: AppConsentTVLog) {
Log.i(
MainActivity::class.java.simpleName,
log.toString()
)
}
})
/*
Add a navigation listener logger (historical)
*/
AppConsentSDK.setNavigationLogListener(object : AppConsentNavigationListener {
override fun onNoticeBackPressed() {
Log.i(
MainActivity::class.java.simpleName,
"onNoticeBackPressed"
)
}
})
/*
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.
- We set setNeedToReplaceUrlViewerByQrCode to true to display QR codes instead of the embedded webview for hyperlinks.
*/
val acConfiguration = ACConfiguration.Builder()
.setForceApplyGDPR(true)
.setNeedToReplaceUrlViewerByQrCode(true)
.build()
AppConsentSDK.initialize(
appKey = "YOUR_APPKEY",
configuration = acConfiguration
) { appConsentInitialized ->
/*
To avoid certain problems, use the instance received by the onReady callback
This has been successfully initialized
*/
appConsent = appConsentInitialized
registerCallbacks()
tryToDisplayCmpAndCheckUpdateIfNeeded()
}
}
private fun registerCallbacks() {
/*
Registers with CMP callback to know when the user
has given consent, or if an error has occurred
*/
appConsent?.setOnPresentNoticeListener(object : OnPresentNoticeListener {
override fun presentConsentError(error: ACError) {
/*
An error has occurred
*/
removeCMPCallback()
}
override fun presentConsentGiven() {
/*
The user has given his consent
*/
removeCMPCallback()
appConsent?.let { appConsentNN ->
updateFirebaseConsent(appConsentNN)
}
}
})
}
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.
*/
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
}
}
}
import com.google.firebase.analytics.FirebaseAnalytics;
import com.sfbx.appconsent.AppConsent;
import com.sfbx.appconsent.core.model.gcm.GCMStatus;
import com.sfbx.appconsent.tv.AppConsentSDK;
import com.sfbx.appconsent.tv.listener.OnPresentNoticeListener;
import com.sfbx.appconsent.tv.model.ACConfiguration;
import com.sfbx.appconsent.tv.model.error.ACError;
import java.util.HashMap;
import java.util.Map;
import kotlin.Unit;
public class MainActivity extends AppCompatActivity {
private AppConsent appConsent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
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();
}
tryToDisplayCmpAndCheckUpdateIfNeeded();
}
}
/**
* 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.
*
* 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 void checkIfNoticeHasBeenUpdated() {
appConsent.checkForUpdate(isNeedToPresentTheCmp -> {
// 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();
/*
Remember to re-register for callbacks if you have previously removed them,
otherwise you will not know when the user has given their consent.
*/
registerCallbacks();
if (false == tryToDisplayCMP()) {
/*
At this stage, if the CMP is not displayed, it is possible, for example:
that the user is no longer in a geographical area subject to GDPR
*/
removeCMPCallback();
}
} 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)
*/
removeCMPCallback();
}
return Unit.INSTANCE;
}, error -> {
/*
An error has occurred
*/
removeCMPCallback();
return Unit.INSTANCE;
});
}
/**
* Try to display the CMP
*
* @return true, if the CMP displaying, false otherwise
*/
private Boolean tryToDisplayCMP() {
/*
Try to display the CMP according to certain rules.
*/
return (appConsent.tryToDisplayNotice(false) == true);
}
private void tryToDisplayCmpAndCheckUpdateIfNeeded() {
/*
Try to display the CMP according to certain rules first.
*/
if (false == tryToDisplayCMP()) {
/*
The user has already given consent;
The user is not part of an area subject to the application of GDPR;
etc.
*/
removeCMPCallback();
/*
The Notice has not been displayed,
so we'll have to check whether it has been updated
and whether it needs to be shown to users again.
*/
checkIfNoticeHasBeenUpdated();
}
}
/*
Initializes the consent management platform module when the activity is created
*/
private void initCmpModule() {
/*
Add an error listener logger
*/
AppConsentSDK.setErrorLogListener(appConsentTVError -> Log.e(MainActivity.class.toString(), appConsentTVError.toString()));
/*
Add an information listener logger
*/
AppConsentSDK.setInformationLogListener(appConsentTVLog -> Log.i(MainActivity.class.toString(), appConsentTVLog.toString()));
/*
Add a navigation listener logger (historical)
*/
AppConsentSDK.setNavigationLogListener(() -> Log.i(MainActivity.class.toString(), "onNoticeBackPressed"));
/*
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.
- We set setNeedToReplaceUrlViewerByQrCode to true to display QR codes instead of the embedded webview for hyperlinks.
*/
final ACConfiguration acConfiguration = new ACConfiguration.Builder()
.setForceApplyGDPR(true)
.setNeedToReplaceUrlViewerByQrCode(true)
.build();
AppConsentSDK.initialize(
"YOUR_APPKEY",
acConfiguration,
appConsentInitialized -> {
/*
To avoid certain problems, use the instance received by the onReady callback
This has been successfully initialized
*/
appConsent = appConsentInitialized;
registerCallbacks();
tryToDisplayCmpAndCheckUpdateIfNeeded();
return Unit.INSTANCE;
});
}
private void registerCallbacks() {
/*
Registers with CMP callback to know when the user
has given consent, or if an error has occurred
*/
appConsent.setOnPresentNoticeListener(new OnPresentNoticeListener() {
@Override
public void presentConsentError(@NonNull ACError acError) {
/*
An error has occurred
*/
removeCMPCallback();
}
@Override
public void presentConsentGiven() {
/*
The user has given his consent
*/
removeCMPCallback();
if (appConsent != null) {
updateFirebaseConsent(appConsent);
}
}
});
}
private void 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.
*/
if (appConsent != null) {
appConsent.setOnPresentNoticeListener(null);
}
}
private void updateFirebaseConsent(AppConsent appConsent) {
// Consent has just been validated by the user.
// Recovers current GCM status (following user consent).
final GCMStatus gcmConsentStatus = appConsent.getGCMConsentStatus();
final FirebaseAnalytics.ConsentStatus newAnalyticsStorage;
if (gcmConsentStatus.isAnalyticsStorageGranted()) newAnalyticsStorage = GRANTED;
else newAnalyticsStorage = DENIED;
final FirebaseAnalytics.ConsentStatus newAdStorage;
if (gcmConsentStatus.isAdStorageGranted()) newAdStorage = GRANTED;
else newAdStorage = DENIED;
final FirebaseAnalytics.ConsentStatus newAdUserData;
if (gcmConsentStatus.isAdUserDataGranted()) newAdUserData = GRANTED;
else newAdUserData = DENIED;
final FirebaseAnalytics.ConsentStatus newAdPersonalization;
if (gcmConsentStatus.isAdPersonalizationGranted()) newAdPersonalization = GRANTED;
else newAdPersonalization = 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
final Map<FirebaseAnalytics.ConsentType, FirebaseAnalytics.ConsentStatus> consentStatus = new HashMap<>();
consentStatus.put(FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE, newAnalyticsStorage);
consentStatus.put(FirebaseAnalytics.ConsentType.AD_STORAGE, newAdStorage);
consentStatus.put(FirebaseAnalytics.ConsentType.AD_USER_DATA, newAdUserData);
consentStatus.put(FirebaseAnalytics.ConsentType.AD_PERSONALIZATION, newAdPersonalization);
FirebaseAnalytics.getInstance(getApplicationContext()).setConsent(consentStatus);
}
}