# Step 3: 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](https://app.appconsent.io)

<figure><img src="https://4229351976-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkyFlZKFaKP4MUM0zILOg%2Fuploads%2F66oLF36aIOado64LfHVW%2FAppKey_Android_SDK.png?alt=media&#x26;token=3095d05c-203c-4317-af96-4b457111c6c9" alt=""><figcaption></figcaption></figure>

### 2. Initialize AppConsent & use it

The second step is to initialize the SDK and wait for it to be started before using it.

{% hint style="success" %}
**TIP**

We recommend you do this in the **application** class, **splashscreen**, or your "**android.intent.category.LAUNCHER**" activity.
{% endhint %}

The idea is to be able to display the CMP as soon as possible, so that you can load your advertising supplier.

{% hint style="info" %}
**INFO**

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
{% endhint %}

{% tabs %}
{% tab title="Mobile / Tablet" %}
{% hint style="info" %}
**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.
{% endhint %}

**Kotlin**

```kotlin
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.tryToDisplayNoticeFromUiContext(uiContext = this@MainActivity, force = 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
                ...
            }
        })
    }
}
```

**Java**

```java
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.tryToDisplayNoticeFromUiContext(this, 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);
    }
}
```

{% endtab %}

{% tab title="TV" %}
{% hint style="info" %}
**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.
{% endhint %}

**Kotlin**

```kotlin
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
                ...
            }
        })
    }
}
```

**Java**

```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;

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);
    }
}
```

{% endtab %}
{% endtabs %}

### 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.

{% tabs %}
{% tab title="Mobile / Tablet" %}
{% hint style="info" %}
**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.
{% endhint %}

**Kotlin**

```kotlin
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()?.tryToDisplayNoticeFromUiContext(uiContext = this@SecondActivity, 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)
    }
}
```

**Java**

```java
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.tryToDisplayNoticeFromUiContext(this, 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);
    }
}
```

{% endtab %}

{% tab title="TV" %}
{% hint style="info" %}
**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.
{% endhint %}

**Kotlin**

```kotlin
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)
    }
}
```

**Java**

```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;

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);
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**NOTE**

Would you like a more complete example?\
We suggest you read the [**Go further**](https://docs.sfbx.io/configuration/step-3-notice-implementation-web-app-tv/android/old-sdk-appconsentsdk/go-further) page.
{% endhint %}
