Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

actually register token on permissions change (#3990)

* actually register token on permissions change

* actually register token on permissions change

* get updated permissions every time

* remove all usages of `usePermissions`

* skip perms check on granted result from request

authored by

Hailey and committed by
GitHub
8e1541e0 d3406c89

+21 -10
+21 -10
src/lib/notifications/notifications.ts
··· 37 37 } 38 38 } 39 39 40 + async function getPushToken(skipPermissionCheck = false) { 41 + const granted = 42 + skipPermissionCheck || (await Notifications.getPermissionsAsync()).granted 43 + if (granted) { 44 + Notifications.getDevicePushTokenAsync() 45 + } 46 + } 47 + 40 48 export function useNotificationsRegistration() { 41 - const [currentPermissions] = Notifications.usePermissions() 42 49 const {getAgent} = useAgent() 43 50 const {currentAccount} = useSession() 44 51 45 52 React.useEffect(() => { 46 - if (!currentAccount || !currentPermissions?.granted) { 53 + if (!currentAccount) { 47 54 return 48 55 } 49 56 50 - // Whenever we all `getDevicePushTokenAsync()`, a change event will be fired below 51 - Notifications.getDevicePushTokenAsync() 57 + getPushToken() 52 58 53 59 // According to the Expo docs, there is a chance that the token will change while the app is open in some rare 54 60 // cases. This will fire `registerPushToken` whenever that happens. ··· 59 65 return () => { 60 66 subscription.remove() 61 67 } 62 - }, [currentAccount, currentPermissions?.granted, getAgent]) 68 + }, [currentAccount, getAgent]) 63 69 } 64 70 65 71 export function useRequestNotificationsPermission() { 66 72 const gate = useGate() 67 - const [currentPermissions] = Notifications.usePermissions() 68 73 69 74 return React.useCallback( 70 75 async (context: 'StartOnboarding' | 'AfterOnboarding') => { 76 + const permissions = await Notifications.getPermissionsAsync() 77 + 71 78 if ( 72 79 !isNative || 73 - currentPermissions?.status === 'granted' || 74 - (currentPermissions?.status === 'denied' && 75 - !currentPermissions?.canAskAgain) 80 + permissions?.status === 'granted' || 81 + (permissions?.status === 'denied' && !permissions?.canAskAgain) 76 82 ) { 77 83 return 78 84 } ··· 93 99 logEvent('notifications:request', { 94 100 status: res.status, 95 101 }) 102 + 103 + if (res.granted) { 104 + // This will fire a pushTokenEvent, which will handle registration of the token 105 + getPushToken(true) 106 + } 96 107 }, 97 - [currentPermissions?.canAskAgain, currentPermissions?.status, gate], 108 + [gate], 98 109 ) 99 110 }