# Consent and opt-out

> Gate the SDK on user consent with setEnabled, record per-channel subscriptions, and know what happens to data queued before a user opts out.

If your app asks users for consent before it collects analytics or sends marketing, connect that choice to the SDK in two places: whether the SDK runs at all, and which channels the user agreed to receive.

## Before you start

- Your app has a consent screen or a consent management platform, and you can read the user's current choice at launch.

## 1. Gate the SDK on consent

Configure the SDK at every launch, then set it on or off from the stored consent in the same place, before anything else runs. The setting is saved, but setting it every launch keeps the SDK in step with your consent store.

:::tabs
::tab ios
```swift
func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
    Herald.configure(appKey: "YOUR_APP_KEY", endpoint: URL(string: "https://YOUR_HERALD_HOST")!)
    Herald.shared.setEnabled(ConsentStore.shared.allowsMessaging)
    return true
}
```
::tab android
```kotlin
class LumenApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        Herald.configure(this, appKey = "YOUR_APP_KEY", endpoint = "https://YOUR_HERALD_HOST")
        Herald.setEnabled(ConsentStore.allowsMessaging(this))
    }
}
```
:::

SDK calls run in the order you make them, so `setEnabled(false)` takes effect before the SDK starts its first session.

**Do not delay `configure` until consent instead.** Calls you make before `configure` are kept in memory and applied once it runs, so a `track` made before consent would be sent after it. Configure early and use `setEnabled`.

## 2. Update it when the user changes their mind

:::tabs
::tab ios
```swift
func consentChanged(allowsMessaging: Bool) {
    Herald.shared.setEnabled(allowsMessaging)
    if allowsMessaging { Herald.shared.flush() }
}
```
::tab android
```kotlin
fun onConsentChanged(allowsMessaging: Boolean) {
    Herald.setEnabled(allowsMessaging)
    if (allowsMessaging) Herald.flush()
}
```
:::

## What happens when the SDK is off

| | While off |
|---|---|
| New calls (`track`, `identify`, `setAttributes` and the rest) | Ignored. They are not queued for later. |
| Events queued before the user opted out | Stay on the device, unsent. They are sent after `setEnabled(true)`. |
| Sessions, message sync, in-app messages | Stopped |
| Content cards already on the device | Hidden: `contentCards` is empty, so the default feed shows nothing. They come back after `setEnabled(true)`. |
| Push notifications | When the SDK turns off, it sends one push unsubscribe for this device, so Herald stops sending it pushes. On Android, `herald-fcm` also stops showing Herald pushes while the SDK is off. |
| Device ID | Kept |

The push unsubscribe is best effort: the SDK tries once, in the background, and does not queue or retry it. It is sent only when the device has a push token, and Herald applies it like any other push unsubscribe (see [Record channel subscriptions](#3-record-channel-subscriptions)): for an identified user whose identity it accepts. If the device is offline at that moment, Herald can keep sending pushes. On iOS the system shows alert pushes without asking the app, so the SDK cannot hide one that still arrives.

After `setEnabled(true)`, if the device still has its push token and notification permission, the SDK sends the token again with a push subscription of subscribed in its next batch, and Herald lifts the unsubscribe it recorded. A suppression from a bounce or one added by an admin stays. After `wipeData()` the device has a new ID and no token. When `setEnabled(true)` runs while notification permission is still granted, the SDK fetches the token again (iOS registers with APNs and gets it back through your `setPushToken` forwarding; Android needs `HeraldMessagingService.syncToken` to have run after `configure`) and sends it with a push subscription of subscribed. Without permission, pushes resume only once the app records the push subscription again: call `requestPushAuthorization()`, or `setSubscription` for push with subscribed or opted in.

If the user withdraws consent and the events already queued must not be sent, call `wipeData()` as well. It sends the same one-time push unsubscribe first, then deletes the queue, the cached cards and messages (they disappear from `contentCards` at once) and every other piece of SDK data on the device. See [Data collection controls](/docs/data-and-privacy/data-collection-controls#wipe-the-device).

:::tabs
::tab ios
```swift
func consentWithdrawn() {
    Herald.shared.wipeData()   // deletes the queue; leaves the SDK off
}
```
::tab android
```kotlin
fun onConsentWithdrawn() {
    Herald.wipeData()          // deletes the queue; leaves the SDK off
}
```
:::

## 3. Record channel subscriptions

Consent to messages is often per channel. Tell Herald with `setSubscription`. The states are `subscribed`, `unsubscribed` and `opted_in` (explicit opt-in, for channels and regions that require it).

:::tabs
::tab ios
```swift
Herald.shared.setEmail("maya@example.com")
Herald.shared.setSubscription(.email, .optedIn)
Herald.shared.setSubscription(.sms, .unsubscribed)
```
::tab android
```kotlin
Herald.setEmail("maya@example.com")
Herald.setSubscription(SubscriptionChannel.EMAIL, SubscriptionState.OPTED_IN)
Herald.setSubscription(SubscriptionChannel.SMS, SubscriptionState.UNSUBSCRIBED)
```
:::

How Herald applies them:

- **Unsubscribed** adds a suppression for the user's addresses on that channel, so Herald stops sending to them. For email and SMS that is the user's email address or phone number. For push it is only this device's push token, because push permission belongs to the device.
- **Subscribed** and **opted in** lift an earlier unsubscribe. They never lift a suppression caused by a bounce or a complaint.
- A subscription applies to addresses Herald already has. Set the email or phone first, in the same session, as in the example.
- Subscriptions are written only for an identified user whose identity Herald accepts. See [Users and identity](/docs/concepts/users-and-identity#user-tokens-and-verification-modes).
- Content cards and in-app messages have no subscription. To stop them, turn the SDK off.
- Turning the SDK off, or calling `wipeData()`, sends a push unsubscribe for the device by itself. See [What happens when the SDK is off](#what-happens-when-the-sdk-is-off).

### Push

`requestPushAuthorization()` records the push subscription for you: opted in when the user allows notifications, unsubscribed when they decline. If the user later turns notifications off in system settings, call `setSubscription(.push, .unsubscribed)` (iOS) or `setSubscription(SubscriptionChannel.PUSH, SubscriptionState.UNSUBSCRIBED)` (Android) when you notice the change, for example at launch. The current permission is also sent in the device context, unless you use minimal mode.

## 4. Record the lawful basis

SDK 0.6.0 and later can record the lawful basis your app collects data under, and the policy it was given under. It is sent with each batch while set, kept on the device and on the person, and shown to admins on the person's page as **Consent basis**.

:::tabs
::tab ios
```swift
Herald.shared.setConsent(basis: .consent, documentId: "privacy-policy", documentVersion: "2026-11")
// When it no longer applies:
Herald.shared.clearConsent()
```
::tab android
```kotlin
Herald.setConsent(ConsentBasis.CONSENT, documentId = "privacy-policy", documentVersion = "2026-11")
// When it no longer applies:
Herald.clearConsent()
```
:::

The bases are the GDPR lawful bases: `consent`, `contract`, `legal_obligation`, `vital_interests`, `public_task` and `legitimate_interests`. `documentId` and `documentVersion` are optional, up to 100 characters each.

This is recorded for your compliance records. It doesn't change what is collected: use the [data collection settings](/docs/data-and-privacy/data-collection-controls) or `setEnabled(false)` for that. After `clearConsent()` the device stops sending it; the person keeps the last record sent.

## Verify it works

1. Turn consent off, use the app, then turn it back on. With debug logging, no events from the "off" period are sent.
2. Unsubscribe from email in the app and send the queue. In Herald, open **Settings > Sending** and the connection: the **Suppressions** card lists the address with the reason `unsubscribe` and the source `sdk`.

## Troubleshooting

- **Events from before consent show up in Herald.** You called the SDK before `configure` and then configured after consent. Configure at launch and gate with `setEnabled`.
- **An unsubscribe has no effect.** The user had no email address, phone number or push token on file yet, or the batch was not verified. Set the address first and check the user token.

## Related

- [Data collection controls](/docs/data-and-privacy/data-collection-controls)
- [User data deletion](/docs/data-and-privacy/user-data-deletion)
- [Permission prompts](/docs/push-notifications/permission-prompts)
