Skip to main content

Integration


How to use AppConsent

1. Get your AppKey from AppConsent : https://app.appconsent.io

GetAppkey

2. Create AppConsent Instance

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.sfbx.appconsentv3.AppConsent
import com.sfbx.appconsentv3.ui.listener.OnPresentNoticeListener


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 (!com.sfbx.appconsentv3.ui.AppConsentSDK.isSdkInitialized()) {
initSfbxModule()
} else {
if (appConsent == null) {
/*
Retrieves the AppConsent instance or null if it has not yet been instantiated, for example
*/
appConsent = com.sfbx.appconsentv3.ui.AppConsentSDK.getInstance()
}
}
}

/*
Initializes the consent management platform module when the activity is created
*/
private fun initSfbxModule() {
com.sfbx.appconsentv3.ui.AppConsentSDK.initialize(
appKey = "{APP_KEY}"
) { appConsentInitialized ->
/*
Use the instance received by the onReady callback
This has been successfully initialized
*/
appConsent = appConsentInitialized
/*
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
*/
}

override fun presentConsentGiven() {
/*
The user has given his consent
*/
}
})
/*
Try to display the CMP according to certain rules.
*/
val isNoticeDisplayed = appConsentInitialized.tryToDisplayNotice(false)
if (!isNoticeDisplayed) {
/*
The user has already given consent;
The user is not part of an area subject to the application of GDPR;
etc.
*/
}
}
}
}

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.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.tryToDisplayNotice(false) // display CMP notice only if needed
appConsent.tryToDisplayNotice(true) // force to display CMP notice
info

tryToDisplayNotice(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 application. To know more about keys used to save your consents, please refer to the IAB documentation.

We also provide an additional key for Google Additionnal Consent IABTCF_AddtlConsent returning a String.

5. Xchange

  • Set xchange user data
appConsent.setXchangeUserData(
XChangeData.Builder()
.email("test@sfbx.io")
.build()
)

Save user data in cache, and when user give his consent, data are sent to server.