Android TV
Get AppConsent TV SDKβ
To add the AppConsent SDK to your Android project, follow the steps below :
Declaration of the repositoryβ
Gradle <6.8β
In the build.gradle
file at the root of your project, add the following:
repositories {
...
maven {
url "https://artifactory.datalf.chat/artifactory/app-consent-v2-release"
}
// To test TCFv2.2
maven {
url "https://artifactory.datalf.chat/artifactory/appconsent-staging"
}
}
Gradle >=6.8β
In your settings.gradle
file, at the root of your project, add the following :
dependencyResolutionManagement {
...
repositories {
...
maven {
url "https://artifactory.datalf.chat/artifactory/app-consent-v2-release"
}
// To test TCFv2.2
maven {
url "https://artifactory.datalf.chat/artifactory/appconsent-staging"
}
}
}
Library integrationβ
In your application build.gradle
add the following:
dependencies {
implementation 'com.sfbx.appconsent:appconsent-tv:${currentTvVersion}'
}
Where currentTvVersion = 1.1.48
To test the new TCF2.2 implementation, please use this dependency
dependencies {
implementation("com.sfbx.appconsent:appconsent-tv-staging:4.0.0")
}
How to use AppConsentβ
1. Get your AppKey from AppConsent : https://app.appconsent.ioβ
2. Create AppConsentTVβ
import com.sfbx.appconsent.tv.AppConsentTV
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val appConsent = AppConsentTV(
context = this,
appKey = "YOUR_APP_KEY"
) {
it.presentNotice(false)
}
}
}
To test the new TCF2.2 integration, please use this snippet
import com.sfbx.appconsent.tv.listener.OnPresentNoticeListener
class MainActivity : FragmentActivity() {
private lateinit var binding: ActivityMainBinding
private var appConsent: com.sfbx.appconsent.AppConsent? = null
private var appKey: String = "{APP_KEY}"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
/*
Shows whether the SDK has already been initialized
*/
if (!com.sfbx.appconsent.tv.AppConsent.isSdkInitialized()) {
com.sfbx.appconsent.tv.AppConsent.sdkInitialize(
appKey = appKey,
) {
/*
Use the instance received by the onReady callback
This has been successfully initialized
*/
appConsent = it
/*
Registers with CMP callback to know when the user
has given consent, or if an error has occurred
*/
it.setOnPresentNoticeListener(object : OnPresentNoticeListener {
override fun presentConsentGiven() {
/*
The user has given his consent
*/
}
override fun presentConsentError(error: Throwable?) {
/*
An error has occurred
*/
}
})
/*
Try to display the CMP according to certain rules.
*/
it.tryDisplayNotice(false).takeIf { !it }?.let {
/*
The user has already given consent;
The user is not part of an area subject to the application of GDPR;
etc.
*/
Toast.makeText(
this@MainActivity.applicationContext,
"The CMP didn't displayed because of no need to do that (maybe language, no need to update, etc.)",
Toast.LENGTH_SHORT
).show()
}
}
}else{
if (appConsent == null) {
/*
Retrieves the AppConsent instance or null if it has not yet been instantiated, for example
*/
appConsent = com.sfbx.appconsent.tv.AppConsent.getInstance()
}
}
}
}
3. Then use appConsent objectβ
- Technical Documentation
- TCF2.2 - Technical Documentation
- Check if user gave consent
appConsent.consentGiven()
Return true
if consent is given, false
otherwise.
- Consent listener
Add Listenerβ
appConsent.addNoticeListener(object : AppConsentNoticeListener {
override fun onConsentGiven() {
// ...
}
override fun onError(error: AppConsentError) {
// ...
}
})
Remove Listenerβ
appConsent.removeNoticeListener(listener)
Every time consent is updated, a new event will be fired in AppConsentNoticeListener
.
- Display CMP notice
appConsent.presentNotice(false) // display CMP notice only if needed
appConsent.presentNotice(true) // force to display CMP notice
info
presentNotice(force : Boolean)
is a local method that only check cache. See checkForUpdate()
to fetch a notice update
- Consentable allowed
appConsent.consentableAllowed(1,0)
Return true
if consentable with id = 1
and consentableType = 0
is allowed, false
otherwise. The id
to pass is the iabId
of your purpose and the consentableType
is the type, e.g: purpose = 0
.
- Stack Allowed
appConsent.stackAllowed(1)
Return true
if stack with id = 1
is allowed, false
otherwise.
- Vendor allowed
appConsent.vendorAllowed(1)
Return true
if vendor with id = 1
is allowed, false
otherwise.
- Set consentable status
appConsent.setConsentableConsents(
mapOf(1 to ConsentStatus.ALLOWED, 2 to ConsentStatus.DISALLOWED),
object : AppConsentSetConsentableConsentsCallback {
override fun onSuccess() {
// ...
}
override fun onError(t: Throwable) {
// ...
}
}
)
Set consentables status, save it and send it to server.
- Check for update
appConsent.checkForUpdate(object : AppConsentUpdateCallback {
override fun onResult(updated: Boolean) {
// ...
}
override fun onError(t: Throwable?) {
// ...
}
})
Check if consent must be updated (new gvl version, new consentables...).
- Clear consents
appConsent.clearConsent()
Clear consents on mobile, but not on server.
- Set external data
val map = mapOf<String, Any>("externalId" to "abze23", "otherData" to "{\"name\": \"test\"}")
appConsent.setExternalData(map)
Set external data and send it to server.
- Get external data
val map: Map<String, Any> = appConsent.getExternalData()
warning
Manifest informations
We have deactivated the automatic backup feature, which allows sensitive data to be sent to the Google Drive (this is automatically activated as long as it is not redefined by the developer).
If your application needs to integrate this feature, simply add these lines:
android:allowBackup="true"
android:fullBackupContent="true"
android:dataExtractionRules="{YOUR_XML_RULES}"
tools:replace="android:allowBackup,android:fullBackupContent,android:dataExtractionRules"
Also refer to the official doc to cover your needs: https://developer.android.com/guide/topics/data/autobackup#xml-syntax-android-12
For more details please refer to the official Google doc about merge rules in Manifest : https://developer.android.com/build/manage-manifests
- Check if user gave consent
appConsent.consentGiven()
Return true
if consent is given, false
otherwise.
- Consent listener
Add Listenerβ
appConsent.setOnPresentNoticeListener(object : OnPresentNoticeListener {
override fun presentConsentGiven() {
// ...
}
override fun presentConsentError(error: Throwable?) {
// ...
}
})
Remove Listenerβ
appConsent.setOnPresentNoticeListener(null)
- Display CMP notice
// @return true if the notice display, false otherwise
appConsent.tryDisplayNotice(false) // display CMP notice only if needed
appConsent.tryDisplayNotice(true) // force to display CMP notice
info
tryDisplayNotice(force : Boolean)
is a local method that only check cache. See checkForUpdate()
to fetch a notice update
- Consentable allowed
appConsent.consentableAllowed(1,0)
Return true
if consentable with id = 1
and consentableType = 0
is allowed, false
otherwise. The id
to pass is the iabId
of your purpose and the consentableType
is the type, e.g: purpose = 0
.
- Stack Allowed
appConsent.stackAllowed(1)
Return true
if stack with id = 1
is allowed, false
otherwise.
- Vendor allowed
appConsent.vendorAllowed(1)
Return true
if vendor with id = 1
is allowed, false
otherwise.
- Set consentable status
appConsent.setConsentableConsents(
mapOf(1 to ConsentStatus.ALLOWED, 2 to ConsentStatus.DISALLOWED),
object : AppConsentSetConsentableConsentsCallback {
override fun onSuccess() {
// ...
}
override fun onError(t: Throwable) {
// ...
}
}
)
Set consentables status, save it and send it to server.
- Check for update
appConsent.checkForUpdate(object : AppConsentUpdateCallback {
override fun onResult(updated: Boolean) {
// ...
}
override fun onError(t: Throwable?) {
// ...
}
})
Check if consent must be updated (new gvl version, new consentables...).
- Clear consents
appConsent.clearConsent()
Clear consents on mobile, but not on server.
- Set external data
val map = mapOf<String, Any>("externalId" to "abze23", "otherData" to "{\"name\": \"test\"}")
appConsent.setExternalData(map)
Set external data and send it to server.
- Get external data
val map: Map<String, Any> = appConsent.getExternalData()
4. Retrieve your consentsβ
Your consents are saved in SharedPreferences
of your TV application. To know more about keys used to save your consents, please refer to the IAB documentation.