Clean up
This commit is contained in:
@@ -20,8 +20,6 @@ async function _registerPushToken(
|
|||||||
account: SessionAccount,
|
account: SessionAccount,
|
||||||
token: Notifications.DevicePushToken,
|
token: Notifications.DevicePushToken,
|
||||||
) {
|
) {
|
||||||
logger.debug(`registerPushToken`)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await agent.app.bsky.notification.registerPush({
|
await agent.app.bsky.notification.registerPush({
|
||||||
serviceDid: account.service?.includes('staging')
|
serviceDid: account.service?.includes('staging')
|
||||||
@@ -56,6 +54,7 @@ const registerPushToken = debounce(_registerPushToken, 100)
|
|||||||
*/
|
*/
|
||||||
async function getPushToken() {
|
async function getPushToken() {
|
||||||
const granted = (await Notifications.getPermissionsAsync()).granted
|
const granted = (await Notifications.getPermissionsAsync()).granted
|
||||||
|
logger.debug(`getPushToken`, {granted})
|
||||||
if (granted) {
|
if (granted) {
|
||||||
return Notifications.getDevicePushTokenAsync()
|
return Notifications.getDevicePushTokenAsync()
|
||||||
}
|
}
|
||||||
@@ -76,7 +75,7 @@ async function getPushToken() {
|
|||||||
* @see https://github.com/expo/expo/issues/28656
|
* @see https://github.com/expo/expo/issues/28656
|
||||||
* @see https://github.com/expo/expo/issues/29909
|
* @see https://github.com/expo/expo/issues/29909
|
||||||
*/
|
*/
|
||||||
async function getAndRegisterPushToken(
|
export async function getAndRegisterPushToken(
|
||||||
agent: AtpAgent,
|
agent: AtpAgent,
|
||||||
currentAccount: SessionAccount,
|
currentAccount: SessionAccount,
|
||||||
) {
|
) {
|
||||||
@@ -87,7 +86,7 @@ async function getAndRegisterPushToken(
|
|||||||
*/
|
*/
|
||||||
const token = await getPushToken()
|
const token = await getPushToken()
|
||||||
|
|
||||||
logger.debug(`getAndRegisterPushToken`, {token})
|
logger.debug(`getAndRegisterPushToken`, {token: token ?? 'undefined'})
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
/**
|
/**
|
||||||
@@ -103,6 +102,13 @@ async function getAndRegisterPushToken(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook to register the device's push notification token with the Bluesky
|
||||||
|
* server, as well as listen for push token updates, should they occurr.
|
||||||
|
*
|
||||||
|
* Registered via the shell, which wraps the navigation stack, meaning if we
|
||||||
|
* have a current account, this handling will be registered and ready to go.
|
||||||
|
*/
|
||||||
export function useNotificationsRegistration() {
|
export function useNotificationsRegistration() {
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
@@ -112,7 +118,11 @@ export function useNotificationsRegistration() {
|
|||||||
|
|
||||||
logger.debug(`useNotificationsRegistration`)
|
logger.debug(`useNotificationsRegistration`)
|
||||||
|
|
||||||
// init push token
|
/**
|
||||||
|
* Init push token, if permissions are granted already. If they weren't,
|
||||||
|
* they'll be requested by the `useRequestNotificationsPermission` hook
|
||||||
|
* below.
|
||||||
|
*/
|
||||||
getAndRegisterPushToken(agent, currentAccount)
|
getAndRegisterPushToken(agent, currentAccount)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,24 +173,27 @@ export function useRequestNotificationsPermission() {
|
|||||||
status: res.status,
|
status: res.status,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (res.granted) {
|
||||||
/**
|
/**
|
||||||
* Note that this will run:
|
* Load bearing. The `addPushTokenListener` will be registered by the
|
||||||
*
|
* time this runs, and this should fire that listener automatically to
|
||||||
* 1) Right after the user signs in, leading to no `currentAccount`
|
* register the token with our server. If for some reason it does not, we
|
||||||
* account being available - this will be instead picked up from the
|
* have a fallback below. See above for more info.
|
||||||
* useEffect above on `currentAccount` change
|
|
||||||
*
|
|
||||||
* 2) Right after onboarding. In this case, we _need_ this
|
|
||||||
* registration, since `currentAccount` will not change and we need to
|
|
||||||
* ensure the token is registered right after permission is granted.
|
|
||||||
* `currentAccount` will already be available in this case, so the
|
|
||||||
* registration will succeed. We should remove this once
|
|
||||||
* expo-notifications (and possibly FCMv1) is fixed and the
|
|
||||||
* `addPushTokenListener` is working again.
|
|
||||||
*/
|
*/
|
||||||
if (res.granted && currentAccount) {
|
const token = await getPushToken()
|
||||||
logger.debug(`useRequestNotificationsPermission`, {context})
|
|
||||||
getAndRegisterPushToken(agent, currentAccount)
|
/**
|
||||||
|
* This call is our insurance policy in case `addPushTokenListener`
|
||||||
|
* didn't fire as a result of `getPushToken`. See comments above for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* Right after login, `currentAccount` in this scope will be undefined,
|
||||||
|
* hence the guard here. For other callsites, it should be defined
|
||||||
|
* already.
|
||||||
|
*/
|
||||||
|
if (token && currentAccount) {
|
||||||
|
registerPushToken(agent, currentAccount, token)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user