diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index f0667b0c..705d90c5 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -71,46 +71,41 @@ export function useNotificationsRegistration() { export function useRequestNotificationsPermission() { const gate = useGate() - const {currentAccount} = useSession() - return React.useCallback( - async (context: 'StartOnboarding' | 'AfterOnboarding' | 'Login') => { - const permissions = await Notifications.getPermissionsAsync() + return async (context: 'StartOnboarding' | 'AfterOnboarding' | 'Login') => { + const permissions = await Notifications.getPermissionsAsync() - if ( - !currentAccount || - !isNative || - permissions?.status === 'granted' || - (permissions?.status === 'denied' && !permissions?.canAskAgain) - ) { - return - } - if ( - context === 'StartOnboarding' && - gate('request_notifications_permission_after_onboarding') - ) { - return - } - if ( - context === 'AfterOnboarding' && - !gate('request_notifications_permission_after_onboarding') - ) { - return - } + if ( + !isNative || + permissions?.status === 'granted' || + permissions?.status === 'denied' + ) { + return + } + if ( + context === 'StartOnboarding' && + gate('request_notifications_permission_after_onboarding_v2') + ) { + return + } + if ( + context === 'AfterOnboarding' && + !gate('request_notifications_permission_after_onboarding_v2') + ) { + return + } - const res = await Notifications.requestPermissionsAsync() - logEvent('notifications:request', { - context: context, - status: res.status, - }) + const res = await Notifications.requestPermissionsAsync() + logEvent('notifications:request', { + context: context, + status: res.status, + }) - if (res.granted) { - // This will fire a pushTokenEvent, which will handle registration of the token - getPushToken(true) - } - }, - [gate, currentAccount], - ) + if (res.granted) { + // This will fire a pushTokenEvent, which will handle registration of the token + getPushToken(true) + } + } } export async function decrementBadgeCount(by: number | 'reset' = 1) { diff --git a/src/lib/statsig/gates.ts b/src/lib/statsig/gates.ts index c572c072..2721871f 100644 --- a/src/lib/statsig/gates.ts +++ b/src/lib/statsig/gates.ts @@ -1,4 +1,4 @@ export type Gate = // Keep this alphabetic please. - | 'request_notifications_permission_after_onboarding' + | 'request_notifications_permission_after_onboarding_v2' | 'show_follow_back_label_v2' diff --git a/src/screens/Onboarding/StepFinished.tsx b/src/screens/Onboarding/StepFinished.tsx index b8a21680..c75dd4fa 100644 --- a/src/screens/Onboarding/StepFinished.tsx +++ b/src/screens/Onboarding/StepFinished.tsx @@ -13,6 +13,7 @@ import {RQKEY as profileRQKey} from '#/state/queries/profile' import {useAgent} from '#/state/session' import {useOnboardingDispatch} from '#/state/shell' import {uploadBlob} from 'lib/api' +import {useRequestNotificationsPermission} from 'lib/notifications/notifications' import { DescriptionText, OnboardingControls, @@ -39,6 +40,7 @@ export function StepFinished() { const [saving, setSaving] = React.useState(false) const queryClient = useQueryClient() const agent = useAgent() + const requestNotificationsPermission = useRequestNotificationsPermission() const finishOnboarding = React.useCallback(async () => { setSaving(true) @@ -72,6 +74,7 @@ export function StepFinished() { : 'default', }) })(), + requestNotificationsPermission('AfterOnboarding'), ]) } catch (e: any) { logger.info(`onboarding: bulk save failed`) @@ -98,7 +101,15 @@ export function StepFinished() { track('OnboardingV2:StepFinished:End') track('OnboardingV2:Complete') logEvent('onboarding:finished:nextPressed', {}) - }, [state, dispatch, onboardDispatch, setSaving, track, agent, queryClient]) + }, [ + state, + queryClient, + agent, + dispatch, + onboardDispatch, + track, + requestNotificationsPermission, + ]) React.useEffect(() => { track('OnboardingV2:StepFinished:Start') diff --git a/src/view/screens/Home.tsx b/src/view/screens/Home.tsx index 1744c665..829cd94e 100644 --- a/src/view/screens/Home.tsx +++ b/src/view/screens/Home.tsx @@ -20,7 +20,6 @@ import { } from '#/state/shell' import {useSelectedFeed, useSetSelectedFeed} from '#/state/shell/selected-feed' import {useOTAUpdates} from 'lib/hooks/useOTAUpdates' -import {useRequestNotificationsPermission} from 'lib/notifications/notifications' import {HomeTabNavigatorParams, NativeStackScreenProps} from 'lib/routes/types' import {FeedPage} from 'view/com/feeds/FeedPage' import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager' @@ -59,8 +58,6 @@ function HomeScreenReady({ preferences: UsePreferencesQueryResponse pinnedFeedInfos: SavedFeedSourceInfo[] }) { - const requestNotificationsPermission = useRequestNotificationsPermission() - const allFeeds = React.useMemo( () => pinnedFeedInfos.map(f => f.feedDescriptor), [pinnedFeedInfos], @@ -74,10 +71,6 @@ function HomeScreenReady({ useSetTitle(pinnedFeedInfos[selectedIndex]?.displayName) useOTAUpdates() - React.useEffect(() => { - requestNotificationsPermission('AfterOnboarding') - }, [requestNotificationsPermission]) - const pagerRef = React.useRef(null) const lastPagerReportedIndexRef = React.useRef(selectedIndex) React.useLayoutEffect(() => {