Skip to main content

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​

GetAppkey

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​

  • 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()

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.