Mobilens 中文

Flow · Onboarding

Notification permission

Every screen in this flow

Before asking

Answer

When
Way out
Next
Behaviour

    Usage

    import * as Notifications from 'expo-notifications';
    import { Linking } from 'react-native';
    import { NotificationPermission, type PermissionApi } from '@/components/mobilens/NotificationPermission';
    
    // expo-notifications: "denied" with canAskAgain false means only Settings can turn it on.
    const toStatus = (p: Notifications.NotificationPermissionsStatus) =>
      p.granted ? 'granted' : p.status === 'undetermined' ? 'undetermined' : p.canAskAgain ? 'denied' : 'blocked';
    
    const permissions: PermissionApi = {
      get: async () => toStatus(await Notifications.getPermissionsAsync()),
      request: async () => toStatus(await Notifications.requestPermissionsAsync()),
      openSettings: () => Linking.openSettings(),
    };
    
    export function NotificationsStep({ next }: { next: (enabled: boolean) => void }) {
      return <NotificationPermission permissions={permissions} appName="Northwind" onDone={next} />;
    }
    

    API

    PropTypeDefaultDescription
    permissions PermissionApi – get, request and openSettings, connected to the platform (see Usage).
    onDone (enabled: boolean) => void – Called when the user moves on, with whether notifications are on.
    appName string – The app's name, used in the copy.
    title? string – Heading on the explanation screen before requesting permission.
    primaryLabel? string 'Turn on notifications' Label for the button that opens the system permission prompt.
    labels? Partial<NotificationPermissionLabels> – The words the screen shows. Pass a partial set to change only some of them.
    initialStep? PermissionStep – Where to start. Defaults to checking the current status; pass a step in tests.
    onStepChange? (step: PermissionStep) => void – Called with every step the screen enters, for analytics or logging.

    Controls in this flow