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 requestzio/stable
parent
d3406c89cf
commit
8e1541e0a5
|
@ -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() {
|
export function useNotificationsRegistration() {
|
||||||
const [currentPermissions] = Notifications.usePermissions()
|
|
||||||
const {getAgent} = useAgent()
|
const {getAgent} = useAgent()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!currentAccount || !currentPermissions?.granted) {
|
if (!currentAccount) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Whenever we all `getDevicePushTokenAsync()`, a change event will be fired below
|
getPushToken()
|
||||||
Notifications.getDevicePushTokenAsync()
|
|
||||||
|
|
||||||
// According to the Expo docs, there is a chance that the token will change while the app is open in some rare
|
// 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.
|
// cases. This will fire `registerPushToken` whenever that happens.
|
||||||
|
@ -59,20 +65,20 @@ export function useNotificationsRegistration() {
|
||||||
return () => {
|
return () => {
|
||||||
subscription.remove()
|
subscription.remove()
|
||||||
}
|
}
|
||||||
}, [currentAccount, currentPermissions?.granted, getAgent])
|
}, [currentAccount, getAgent])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useRequestNotificationsPermission() {
|
export function useRequestNotificationsPermission() {
|
||||||
const gate = useGate()
|
const gate = useGate()
|
||||||
const [currentPermissions] = Notifications.usePermissions()
|
|
||||||
|
|
||||||
return React.useCallback(
|
return React.useCallback(
|
||||||
async (context: 'StartOnboarding' | 'AfterOnboarding') => {
|
async (context: 'StartOnboarding' | 'AfterOnboarding') => {
|
||||||
|
const permissions = await Notifications.getPermissionsAsync()
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!isNative ||
|
!isNative ||
|
||||||
currentPermissions?.status === 'granted' ||
|
permissions?.status === 'granted' ||
|
||||||
(currentPermissions?.status === 'denied' &&
|
(permissions?.status === 'denied' && !permissions?.canAskAgain)
|
||||||
!currentPermissions?.canAskAgain)
|
|
||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -93,7 +99,12 @@ export function useRequestNotificationsPermission() {
|
||||||
logEvent('notifications:request', {
|
logEvent('notifications:request', {
|
||||||
status: res.status,
|
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],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue