Handle permission requests in flows - Flutter

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 Flutter SDK 4.0.0 or later: Earlier versions don’t have AdaptyUISystemRequestsHandler. See Install & configure SDK.

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

  • A library that shows the notification dialog: The example uses permission_handler. Any other package or your own platform code works the same way.

  • 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:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
        <!-- ... -->
    </manifest>
  • For iOS with CocoaPods: Add PERMISSION_NOTIFICATIONS=1 to GCC_PREPROCESSOR_DEFINITIONS in your Podfile, as the permission_handler setup describes. Without it, Permission.notification.request() returns denied, and iOS shows no dialog. With Swift Package Manager, permission_handler includes the notification permission by default.

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, Permission.notification.request() returns the earlier answer 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, create a class that extends AdaptyUISystemRequestsHandler and implement handlePermission. Return denied for the permissions your app doesn’t request.

import 'package:adapty_flutter/adapty_flutter.dart';
import 'package:permission_handler/permission_handler.dart';

class FlowSystemRequestsHandler extends AdaptyUISystemRequestsHandler {
  @override
  Future<AdaptyUIPermissionResult> handlePermission(
    AdaptyUIFlowView view,
    AdaptyUIPermission permission,
    Map<String, String>? customArgs,
  ) async {
    // Answer "denied" to every permission your app doesn't request
    if (permission != AdaptyUIPermission.push) {
      return const AdaptyUIPermissionResult.denied();
    }

    try {
      final status = await Permission.notification.request();
      return status.isGranted
          ? const AdaptyUIPermissionResult.granted()
          : const AdaptyUIPermissionResult.denied();
    } catch (_) {
      return const AdaptyUIPermissionResult.denied();
    }
  }

  // The base method does nothing: this keeps the native review prompt.
  @override
  Future<void> handleAppReviewRequest(AdaptyUIFlowView view) {
    return AdaptyUI().requestAppReview();
  }
}

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.

If handlePermission throws or never completes, the flow gets no answer and stays on the screen. The example catches errors for this reason.

A granted permission lets your app show notifications. To receive push notifications, register the device with your push provider, as your app does outside flows.

With a handler registered, the Adapty SDK passes app review requests to handleAppReviewRequest, whose base method does nothing. The example keeps the native review prompt.

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 the permission requests from all flows to your handler, call AdaptyUI().setSystemRequestsHandler once, before you present a flow. For example, call it right after you set the flow events observer:

AdaptyUI().setSystemRequestsHandler(FlowSystemRequestsHandler());

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, and that the iOS Podfile enables PERMISSION_NOTIFICATIONS.