Check canAskAgain for notification permissions (#4460)

This commit is contained in:
Hailey 2024-06-10 12:14:00 -07:00 committed by GitHub
parent 90ec22a674
commit 5dd195bcb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 3 deletions

View file

@ -72,14 +72,17 @@ export function useNotificationsRegistration() {
export function useRequestNotificationsPermission() { export function useRequestNotificationsPermission() {
const gate = useGate() const gate = useGate()
const {currentAccount} = useSession()
return async (context: 'StartOnboarding' | 'AfterOnboarding' | 'Login') => { return async (
context: 'StartOnboarding' | 'AfterOnboarding' | 'Login' | 'Home',
) => {
const permissions = await Notifications.getPermissionsAsync() const permissions = await Notifications.getPermissionsAsync()
if ( if (
!isNative || !isNative ||
permissions?.status === 'granted' || permissions?.status === 'granted' ||
permissions?.status === 'denied' (permissions?.status === 'denied' && !permissions.canAskAgain)
) { ) {
return return
} }
@ -95,6 +98,9 @@ export function useRequestNotificationsPermission() {
) { ) {
return return
} }
if (context === 'Home' && !currentAccount) {
return
}
const res = await Notifications.requestPermissionsAsync() const res = await Notifications.requestPermissionsAsync()
logEvent('notifications:request', { logEvent('notifications:request', {

View file

@ -17,7 +17,7 @@ export type LogEvents = {
} }
'notifications:openApp': {} 'notifications:openApp': {}
'notifications:request': { 'notifications:request': {
context: 'StartOnboarding' | 'AfterOnboarding' | 'Login' context: 'StartOnboarding' | 'AfterOnboarding' | 'Login' | 'Home'
status: 'granted' | 'denied' | 'undetermined' status: 'granted' | 'denied' | 'undetermined'
} }
'state:background:sampled': { 'state:background:sampled': {

View file

@ -20,6 +20,7 @@ import {
} from '#/state/shell' } from '#/state/shell'
import {useSelectedFeed, useSetSelectedFeed} from '#/state/shell/selected-feed' import {useSelectedFeed, useSetSelectedFeed} from '#/state/shell/selected-feed'
import {useOTAUpdates} from 'lib/hooks/useOTAUpdates' import {useOTAUpdates} from 'lib/hooks/useOTAUpdates'
import {useRequestNotificationsPermission} from 'lib/notifications/notifications'
import {HomeTabNavigatorParams, NativeStackScreenProps} from 'lib/routes/types' import {HomeTabNavigatorParams, NativeStackScreenProps} from 'lib/routes/types'
import {FeedPage} from 'view/com/feeds/FeedPage' import {FeedPage} from 'view/com/feeds/FeedPage'
import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager' import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager'
@ -67,10 +68,15 @@ function HomeScreenReady({
const maybeFoundIndex = allFeeds.indexOf(rawSelectedFeed) const maybeFoundIndex = allFeeds.indexOf(rawSelectedFeed)
const selectedIndex = Math.max(0, maybeFoundIndex) const selectedIndex = Math.max(0, maybeFoundIndex)
const selectedFeed = allFeeds[selectedIndex] const selectedFeed = allFeeds[selectedIndex]
const requestNotificationsPermission = useRequestNotificationsPermission()
useSetTitle(pinnedFeedInfos[selectedIndex]?.displayName) useSetTitle(pinnedFeedInfos[selectedIndex]?.displayName)
useOTAUpdates() useOTAUpdates()
React.useEffect(() => {
requestNotificationsPermission('Home')
}, [requestNotificationsPermission])
const pagerRef = React.useRef<PagerRef>(null) const pagerRef = React.useRef<PagerRef>(null)
const lastPagerReportedIndexRef = React.useRef(selectedIndex) const lastPagerReportedIndexRef = React.useRef(selectedIndex)
React.useLayoutEffect(() => { React.useLayoutEffect(() => {