Handle permission requests in flows - Capacitor

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 the onRequestPermission handler in your app. Your app shows the dialog and returns the user’s answer, and the flow continues.

Before you start

You need:

  • Adapty Capacitor SDK 4.1.0 or later: Version 4.1.0 is the first stable release with the onRequestPermission handler.

  • 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 the @capacitor/push-notifications plugin. Any other plugin that shows the dialog works too.

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

    <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 your onRequestPermission handler with the permission push.
  3. Your app shows the system dialog and resolves the handler’s promise to { status: 'granted' } or { status: 'denied' }.
  4. The flow runs the After response actions. They are the same for both answers.

The flow waits on the screen until your handler’s promise resolves. If the promise never resolves, the flow stays on the screen. If the OS doesn’t show the dialog, for example because the user already answered it, PushNotifications.requestPermissions() returns the current status at once, and the flow runs the After response actions right away. On Android 12 and earlier, apps need no runtime permission for notifications, so the plugin returns granted without a dialog. If the After response 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

The onRequestPermission handler is asynchronous. It returns a promise that resolves to an object with the user’s answer:

import type { FlowEventHandlers } from '@adapty/capacitor';
import { PushNotifications } from '@capacitor/push-notifications';

export const onRequestPermission: FlowEventHandlers['onRequestPermission'] = async (permission) => {
  if (permission !== 'push') {
    // Answer every request: the flow waits on the screen until it gets the answer.
    return { status: 'denied' };
  }

  // Shows the system dialog, or returns the current status without showing it.
  const { receive } = await PushNotifications.requestPermissions();
  return { status: receive === 'granted' ? 'granted' : 'denied' };
};

onRequestPermission receives:

  • permission: The permission the flow requests. The Request permission action sends push. Return denied for any other value, so the flow continues.
  • customArgs: Custom arguments of the request. The Request permission action sends none, so the object is empty.

Resolve the promise to { status: 'granted' } only when the user allowed notifications. The Adapty SDK answers denied when your handler throws an error or its promise rejects.

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.

Register the handler

Warning

Without an onRequestPermission handler, the user is never asked. On a flow view without the handler, the Adapty SDK answers denied without a dialog and writes a warning to its log, and the flow runs the After response actions right away.

Set the handler on every flow view your app creates, together with your other flow event handlers, before you present the view. Flow authors can add the Request permission action to any flow later, without an app update, and a flow view without the handler answers denied without asking the user.

import { createFlowView } from '@adapty/capacitor';
import { onRequestPermission } from './permissions';

const view = await createFlowView(flow);

await view.setEventHandlers({
  onRequestPermission,
});

await view.present();

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 flow moves on without a dialog, make sure that the flow view has the onRequestPermission handler, and that the Android app declares POST_NOTIFICATIONS and targets Android 13 or later.