---
title: "Handle permission requests in flows - Kotlin Multiplatform"
description: "Ask users for push notification permission from a flow in your Kotlin Multiplatform app with AdaptyUISystemRequestsHandler, and return their answer to the flow."
---

> **AI agents**: to search Adapty docs faster and with fewer tokens, install the Adapty skills: `npx adapty skills install --yes`

A [flow](adapty-flow-builder) can ask users for permission to send push notifications with the **Request permission** action, for example on an onboarding screen that explains what the notifications are for. The Adapty SDK doesn't show the system permission dialog itself. It passes the request to an `AdaptyUISystemRequestsHandler` in your app. Your app shows the dialog and returns the user's answer, and the flow continues.

## Before you start

You need:

- **Adapty Kotlin Multiplatform SDK 4.1.0 or later**: Version 4.1.0 is the first stable release with `AdaptyUISystemRequestsHandler`.
- **A flow with the Request permission action**: In the Flow & Paywall Builder, add the **Request permission** action to an element's **On Tap** or **On Screen Appear** trigger. In its **After response** section, add the actions to run after the user answers, such as **Navigate Next**. For the action settings, see [Request permission](onboarding-actions#request-permission).
- **The `POST_NOTIFICATIONS` permission in your Android manifest, and `targetSdk` 33 or higher**: Android 13 and later show the notification permission dialog only to apps that declare the permission and target Android 13 or later:

  ```xml title="AndroidManifest.xml"
  <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  ```

## How the flow requests a permission

1. The flow reaches the **Request permission** action: the user taps the element, or the screen appears.
2. The Adapty SDK calls `handlePermission` on your handler with `AdaptyUIPermission.PUSH`.
3. Your app requests the permission from the OS and returns `AdaptyUIPermissionResult.granted()` or `AdaptyUIPermissionResult.denied()`.
4. The flow runs the **After response** actions. They are the same for both answers.

The flow waits on the screen until your app returns the answer. If your app never answers, the flow stays on the screen. If the OS doesn't show the dialog, for example because the user already answered it, your handler returns at once, and the flow runs the **After response** actions right away. If those actions leave the screen, the user sees the screen that requests the permission only for a moment.

With **On Screen Appear**, the flow doesn't send the request again when the user comes back to the screen with a **Navigate back** action. With **On Tap**, every tap sends a new request, except taps while a request waits for your answer.

## Implement the handler

To answer permission requests, implement `AdaptyUISystemRequestsHandler` in common code. Return a result for every request: return `denied` for the permissions your app doesn't request, and catch the exceptions of your platform code. The Adapty SDK doesn't catch exceptions from `handlePermission`: an uncaught exception crashes the app, and the flow gets no answer.

```kotlin showLineNumbers title="commonMain"
import com.adapty.kmp.AdaptyUISystemRequestsHandler
import com.adapty.kmp.models.AdaptyUIFlowView
import com.adapty.kmp.models.AdaptyUIPermission
import com.adapty.kmp.models.AdaptyUIPermissionResult
import kotlin.coroutines.cancellation.CancellationException

class MySystemRequestsHandler : AdaptyUISystemRequestsHandler {
    override suspend fun handlePermission(
        view: AdaptyUIFlowView,
        permission: AdaptyUIPermission,
        customArgs: Map<String, String>?,
    ): AdaptyUIPermissionResult {
        if (permission != AdaptyUIPermission.PUSH) {
            return AdaptyUIPermissionResult.denied()
        }
        return try {
            if (requestPushPermission()) {
                AdaptyUIPermissionResult.granted()
            } else {
                AdaptyUIPermissionResult.denied()
            }
        } catch (e: CancellationException) {
            throw e
        } catch (e: Exception) {
            AdaptyUIPermissionResult.denied(e.message)
        }
    }
}
```

`requestPushPermission` is your platform code. See [Request the permission on each platform](#request-the-permission-on-each-platform).

`handlePermission` receives:

- **`view`**: The flow view that sent the request.
- **`permission`**: The permission the flow requests. The **Request permission** action sends `AdaptyUIPermission.PUSH`.
- **`customArgs`**: Custom arguments of the request. The **Request permission** action sends none, so the value is `null`.

The `detail` string you can pass to `granted` and `denied` is optional.

A granted permission lets your app show notifications. To receive push notifications, register the device with your push provider, such as APNs or Firebase Cloud Messaging, as your app does outside flows.

`AdaptyUISystemRequestsHandler` also has `handleAppReviewRequest`. Its default shows the native review prompt, so you don't need to override it.

## Request the permission on each platform

Common code can't show the system dialog, so `requestPushPermission` is an `expect` function with an `actual` implementation for each platform. If your app already uses a Kotlin Multiplatform permissions library, call it from `handlePermission` instead.

```kotlin title="commonMain"
expect suspend fun requestPushPermission(): Boolean
```

On iOS, ask `UNUserNotificationCenter` for authorization:

```kotlin showLineNumbers title="iosMain"
import kotlinx.coroutines.suspendCancellableCoroutine
import platform.UserNotifications.UNAuthorizationOptionAlert
import platform.UserNotifications.UNAuthorizationOptionBadge
import platform.UserNotifications.UNAuthorizationOptionSound
import platform.UserNotifications.UNUserNotificationCenter
import kotlin.coroutines.resume

actual suspend fun requestPushPermission(): Boolean =
    suspendCancellableCoroutine { continuation ->
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
            UNAuthorizationOptionAlert or UNAuthorizationOptionBadge or UNAuthorizationOptionSound,
        ) { granted, _ ->
            continuation.resume(granted)
        }
    }
```

On Android, the runtime permission exists from Android 13. On earlier versions, return whether notifications are turned on for the app. Ask from the activity that is on screen, which is the flow's activity while a flow is shown. In this example, `ResumedActivity` tracks it. Register it in `Application.onCreate` with `registerActivityLifecycleCallbacks(ResumedActivity)`:

```kotlin showLineNumbers title="androidMain"
import android.Manifest
import android.app.Activity
import android.app.Application
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

object ResumedActivity : Application.ActivityLifecycleCallbacks {
    var current: ComponentActivity? = null
        private set

    override fun onActivityResumed(activity: Activity) {
        current = activity as? ComponentActivity
    }

    override fun onActivityPaused(activity: Activity) {
        if (current === activity) current = null
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
    override fun onActivityStarted(activity: Activity) {}
    override fun onActivityStopped(activity: Activity) {}
    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
    override fun onActivityDestroyed(activity: Activity) {}
}

actual suspend fun requestPushPermission(): Boolean {
    val activity = ResumedActivity.current ?: return false
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
        return NotificationManagerCompat.from(activity).areNotificationsEnabled()
    }
    val permission = Manifest.permission.POST_NOTIFICATIONS
    if (ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED) {
        return true
    }
    return suspendCancellableCoroutine { continuation ->
        lateinit var launcher: ActivityResultLauncher<String>
        launcher = activity.activityResultRegistry.register(
            "push_permission",
            ActivityResultContracts.RequestPermission(),
        ) { granted ->
            launcher.unregister()
            continuation.resume(granted)
        }
        launcher.launch(permission)
        continuation.invokeOnCancellation { launcher.unregister() }
    }
}
```

## Register the handler

:::warning
**Without a handler, the flow stops at the permission request.** If your app registers no handler, the Adapty SDK sends the flow no answer, on iOS and on Android. The flow stays on the screen that sent the request and doesn't run the **After response** actions.
:::

To send permission requests to your handler, call `AdaptyUI.setSystemRequestsHandler` once at app startup, before your app shows the first flow. For example, call it where you call `AdaptyUI.setFlowsEventsObserver`.

```kotlin showLineNumbers title="Kotlin"
import com.adapty.kmp.AdaptyUI

AdaptyUI.setSystemRequestsHandler(MySystemRequestsHandler())
```

## Test the permission request

iOS shows the notification dialog only once per app install. Android stops showing it after the user allows notifications or denies them twice. To see the dialog again while you test the flow, delete the app from the device and install it again. Then open the flow and reach the **Request permission** action. If the screen stays and no dialog appears, the flow got no answer: make sure that your app registers the handler before it presents the flow. If the flow moves on without a dialog, make sure that the Android app declares `POST_NOTIFICATIONS` and targets Android 13 or later.