# How to use it?

***

## How to use appConsent object?

Here is a list of the most commonly used methods

### **Check if user gave consent**

```kotlin
appConsent.consentGiven()
```

Return `true` if consent is given, `false` otherwise.<br>

{% hint style="danger" %}
**WARNING**

Please note that we are only talking about whether the user has confirmed a **choice** and not whether he has **accepted** or **refused** consent.
{% endhint %}

### **Define the listener related to user consent**

When the user has completed the consent process and given their consent, this "callback" informs them of this.

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

It is recommended to define it before trying to display the CMP and to remove it once consent has been given or the cmp has not been displayed.
{% endhint %}

```kotlin
appConsent.setOnPresentNoticeListener(object : OnPresentNoticeListener { 
    override fun presentConsentGiven() {
        // ...
    }

    override fun presentConsentError(error: Throwable?) {
        // ...
    }
})
```

### **Remove Listener**

```kotlin
appConsent.setOnPresentNoticeListener(null)
```

### **Try to Display CMP notice**

There are 2 display modes:

1. The first runs only if user consent is required.
2. The second, which should be used to allow your users to consult/modify their consent *(often used from your settings screen to display your user's privacy policy)*

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

By default this method tries to display the CMP. It tries because, depending on the region of your users (if no settings are made via ACConfiguration to force the display) then it will follow its controls and display the CMP only if necessary.

It will also be displayed if the user consent has not yet been given or if it needs to be renewed.
{% endhint %}

```kotlin
// @return true if the notice display, false otherwise
appConsent.tryToDisplayNoticeFromUiContext(uiContext, false) // display CMP notice only if needed
appConsent.tryToDisplayNoticeFromUiContext(uiContext, true) // force to display CMP notice
```

It returns `true` if the CMP is displayed, `false` otherwise

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

These same methods also exist without the context parameter.

\
These methods do the same thing, except that they implement the `FLAG_ACTIVITY_NEW_TASK` flag to be called from a context other than an activity.

```kotlin
// @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
```

{% endhint %}

### **GCM Status**

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

This method shows the **current** status of GCMv2 (Google Consent Mode V2).

Before calling up this method, it's best to make sure that the user has **already given his consent** and that it's up to date, and that the CMP doesn't need to be redisplayed.

Otherwise :

* either the saved value of the **old consent** will be returned
* or the **default values** of your FirebaseAnalytics AndroidManifest configuration will be returned

[Set the default consent state from google documentation](https://developers.google.com/tag-platform/security/guides/app-consent?consentmode=advanced\&platform=android#default-consent)
{% endhint %}

```kotlin
appConsent.getGCMConsentStatus()
```

Return `GCMStatus`

<details>

<summary>Data model representing the state of GCMv2 (Google Consent Mode v2)</summary>

This will allow you to define consent from your Firebase Analytics instance.

`isAnalyticsStorageGranted`, indicates whether the user has given consent for **FirebaseAnalytics.ConsentType#ANALYTICS\_STORAGE**

`isAdStorageGranted`, indicates whether the user has given consent for **FirebaseAnalytics.ConsentType#AD\_STORAGE**

`isAdUserDataGranted`, indicates whether the user has given consent for **FirebaseAnalytics.ConsentType#AD\_USER\_DATA**

`isAdPersonalizationGranted`, indicates whether the user has given consent for **FirebaseAnalytics.ConsentType#AD\_PERSONALIZATION**

[FirebaseAnalytics.ConsentType](https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.ConsentType)

</details>

### **Check for update**

This method allows you to check from our servers whether your Notice has been updated since it was last displayed on your user's device.

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

The method will return true if you have modified the Source and/or Notice from your dashboard and, if and **only if**, you have configured your Notice to update for all your users.
{% endhint %}

<figure><img src="https://4229351976-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkyFlZKFaKP4MUM0zILOg%2Fuploads%2Fr0pByhnZyUtDmk5qVLRD%2Fcheck_for_update_notice.png?alt=media&#x26;token=37298ec4-ac21-4c66-8914-69911b1c43ae" alt=""><figcaption></figcaption></figure>

{% hint style="danger" %}
**ATTENTION**

This method can only be used once every **30 minutes**. This allows you to force a first network call to make sure your users are up to date. The next calls to this method will use the cache of the previous response or attempt a network call if the previous one was in error.
{% endhint %}

```kotlin
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()
                appConsent.tryToDisplayNotice(false)
            } 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)
                 */
            }
        }) { _: Throwable? ->
            /*
             An error has occurred
             */
        }
```

## Using AppConsent's more specific methods

Here's a list of methods that could be useful if you want to go further in tracking user consent.

### **Consentable allowed**

```kotlin
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**

```kotlin
appConsent.stackAllowed(1)
```

Return `true` if stack with `id = 1` is allowed, `false` otherwise.

### **Vendor allowed**

```kotlin
appConsent.vendorAllowed(1)
```

Return `true` if vendor with `id = 1` is allowed, `false` otherwise.

### **All Consentables Allowed**

```kotlin
appConsent.isAllConsentablesAllowed()
```

Returns `true` if all consentables have been allowed `false` if at least one consentable is not allowed and `null` if no choice has yet been made (notice not yet downloaded, choice not yet made, application cache deleted, etc.).

### **All Consentables Disallowed**

```kotlin
appConsent.isAllConsentablesDisallowed()
```

Returns `true` if all consentables have been disallowed `false` if at least one consentable is not disallowed and `null` if no choice has yet been made (notice not yet downloaded, choice not yet made, application cache deleted, etc.).

### **All Stacks Allowed**

```kotlin
appConsent.isAllStacksAllowed()
```

Returns `true` if all stacks have been accepted `false` if at least one stack is not accepted and `null` if no choice has yet been made or not present into your notice (notice not yet downloaded, choice not yet made, application cache deleted, etc.).

### **All Stacks Disallowed**

```kotlin
appConsent.isAllStacksDisallowed()
```

Returns `true` if all stacks have been disallowed `false` if at least one stack is not disallowed and `null` if no choice has yet been made or not present into your notice (notice not yet downloaded, choice not yet made, application cache deleted, etc.).

### **All Vendors Allowed**

```kotlin
appConsent.isAllVendorsAllowed()
```

Returns `true` if all vendors have been allowed `false` if at least one vendor is not allowed and `null` if no choice has yet been made (notice not yet downloaded, choice not yet made, application cache deleted, etc.).

### **All Vendors Disallowed**

```kotlin
appConsent.isAllVendorsDisallowed()
```

Returns `true` if all vendors have been disallowed `false` if at least one vendor is not disallowed and `null` if no choice has yet been made (notice not yet downloaded, choice not yet made, application cache deleted, etc.).

### **User Accept All**

```kotlin
appConsent.isUserAcceptAll()
```

Returns `true` if all consent items, stacks and vendors are allowed. `false` if at least one of them is not allowed and `null` if no data is present yet (notice not yet downloaded, choice not yet made, application cache deleted, etc.).

### **User Deny All**

```kotlin
appConsent.isUserDenyAll()
```

Returns `true` if all consent items, stacks and vendors are disallowed. `false` if at least one of them is not disallowed and `null` if no data is present yet (notice not yet downloaded, choice not yet made, application cache deleted, etc.).

### **Set consentable status**

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

### **Clear consents**

Locally removes user consent, but not on the server (this will allow a new display of the CMP on the next call to `tryToDisplayNotice(false)` for example)

```kotlin
appConsent.clearConsent()
```

### **Set external ids**

```kotlin
val ids = mapOf<String, String>("customPersonalId" to "abze23", "otherData" to "{\"name\": \"test\"}")
appConsent.setExternalIds(ids)
```

Allows to define additional Ids that will be taken into account when validating user consent.

### **Get external ids**

Retrieves your previously registered external ids

```kotlin
val ids: Map<String, String> = appConsent.getExternalIds()
```

## (Bonus) 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**](https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/TCFv2/IAB%20Tech%20Lab%20-%20CMP%20API%20v2.md#in-app-details).

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