Check `canAskAgain` for notification permissions (#4460)

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

View File

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

View File

@ -20,6 +20,7 @@ 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'
@ -67,10 +68,15 @@ function HomeScreenReady({
const maybeFoundIndex = allFeeds.indexOf(rawSelectedFeed)
const selectedIndex = Math.max(0, maybeFoundIndex)
const selectedFeed = allFeeds[selectedIndex]
const requestNotificationsPermission = useRequestNotificationsPermission()
useSetTitle(pinnedFeedInfos[selectedIndex]?.displayName)
useOTAUpdates()
React.useEffect(() => {
requestNotificationsPermission('Home')
}, [requestNotificationsPermission])
const pagerRef = React.useRef<PagerRef>(null)
const lastPagerReportedIndexRef = React.useRef(selectedIndex)
React.useLayoutEffect(() => {