diff options
author | Hailey <me@haileyok.com> | 2024-05-13 11:11:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-13 11:11:35 -0700 |
commit | 8e1541e0a5eedb67b5a53e297b4734167b8dcb6e (patch) | |
tree | 8586ea57c335dd9a508916fbe10cb1df3dcfd1c8 /src | |
parent | d3406c89cf0c5b46197a87298dcd4e1326fef643 (diff) | |
download | voidsky-8e1541e0a5eedb67b5a53e297b4734167b8dcb6e.tar.zst |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/notifications/notifications.ts | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 66cedeaa6..18578c0c4 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -37,18 +37,24 @@ async function registerPushToken( } } +async function getPushToken(skipPermissionCheck = false) { + const granted = + skipPermissionCheck || (await Notifications.getPermissionsAsync()).granted + if (granted) { + Notifications.getDevicePushTokenAsync() + } +} + export function useNotificationsRegistration() { - const [currentPermissions] = Notifications.usePermissions() const {getAgent} = useAgent() const {currentAccount} = useSession() React.useEffect(() => { - if (!currentAccount || !currentPermissions?.granted) { + if (!currentAccount) { return } - // Whenever we all `getDevicePushTokenAsync()`, a change event will be fired below - Notifications.getDevicePushTokenAsync() + getPushToken() // According to the Expo docs, there is a chance that the token will change while the app is open in some rare // cases. This will fire `registerPushToken` whenever that happens. @@ -59,20 +65,20 @@ export function useNotificationsRegistration() { return () => { subscription.remove() } - }, [currentAccount, currentPermissions?.granted, getAgent]) + }, [currentAccount, getAgent]) } export function useRequestNotificationsPermission() { const gate = useGate() - const [currentPermissions] = Notifications.usePermissions() return React.useCallback( async (context: 'StartOnboarding' | 'AfterOnboarding') => { + const permissions = await Notifications.getPermissionsAsync() + if ( !isNative || - currentPermissions?.status === 'granted' || - (currentPermissions?.status === 'denied' && - !currentPermissions?.canAskAgain) + permissions?.status === 'granted' || + (permissions?.status === 'denied' && !permissions?.canAskAgain) ) { return } @@ -93,7 +99,12 @@ export function useRequestNotificationsPermission() { logEvent('notifications:request', { status: res.status, }) + + if (res.granted) { + // This will fire a pushTokenEvent, which will handle registration of the token + getPushToken(true) + } }, - [currentPermissions?.canAskAgain, currentPermissions?.status, gate], + [gate], ) } |