This commit is contained in:
Eric Bailey
2025-06-24 18:09:24 -05:00
parent d54907eca0
commit c79234ec03
+36 -23
View File
@@ -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
* 1) Right after the user signs in, leading to no `currentAccount` * time this runs, and this should fire that listener automatically to
* account being available - this will be instead picked up from the * register the token with our server. If for some reason it does not, we
* useEffect above on `currentAccount` change * have a fallback below. See above for more info.
* */
* 2) Right after onboarding. In this case, we _need_ this const token = await getPushToken()
* 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 * This call is our insurance policy in case `addPushTokenListener`
* registration will succeed. We should remove this once * didn't fire as a result of `getPushToken`. See comments above for more
* expo-notifications (and possibly FCMv1) is fixed and the * details.
* `addPushTokenListener` is working again. *
*/ * Right after login, `currentAccount` in this scope will be undefined,
if (res.granted && currentAccount) { * hence the guard here. For other callsites, it should be defined
logger.debug(`useRequestNotificationsPermission`, {context}) * already.
getAndRegisterPushToken(agent, currentAccount) */
if (token && currentAccount) {
registerPushToken(agent, currentAccount, token)
}
} }
} }
} }