Handle permission requests in flows - Kotlin Multiplatform
A flow 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.
-
The
POST_NOTIFICATIONSpermission in your Android manifest, andtargetSdk33 or higher: Android 13 and later show the notification permission dialog only to apps that declare the permission and target Android 13 or later:<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
How the flow requests a permission
- The flow reaches the Request permission action: the user taps the element, or the screen appears.
- The Adapty SDK calls
handlePermissionon your handler withAdaptyUIPermission.PUSH. - Your app requests the permission from the OS and returns
AdaptyUIPermissionResult.granted()orAdaptyUIPermissionResult.denied(). - 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.
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.
handlePermission receives:
view: The flow view that sent the request.permission: The permission the flow requests. The Request permission action sendsAdaptyUIPermission.PUSH.customArgs: Custom arguments of the request. The Request permission action sends none, so the value isnull.
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.
expect suspend fun requestPushPermission(): Boolean
On iOS, ask UNUserNotificationCenter for authorization:
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):
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
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.
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.