Formatting nits

This commit is contained in:
Eric Bailey
2025-06-24 15:23:01 -05:00
parent cd820709b6
commit e52478e032
2 changed files with 55 additions and 36 deletions
+55 -35
View File
@@ -1,39 +1,41 @@
import React from 'react' import React from 'react'
import {Platform} from 'react-native'
import * as Notifications from 'expo-notifications' import * as Notifications from 'expo-notifications'
import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications' import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications'
import {BskyAgent} from '@atproto/api' import {type AtpAgent} from '@atproto/api'
import {logEvent} from '#/lib/statsig/statsig'
import {Logger} from '#/logger' import {Logger} from '#/logger'
import {devicePlatform, isAndroid, isNative} from '#/platform/detection' import {isAndroid, isNative} from '#/platform/detection'
import {SessionAccount, useAgent, useSession} from '#/state/session' import {type SessionAccount, useAgent, useSession} from '#/state/session'
import BackgroundNotificationHandler from '../../../modules/expo-background-notification-handler' import BackgroundNotificationHandler from '../../../modules/expo-background-notification-handler'
const SERVICE_DID = (serviceUrl?: string) =>
serviceUrl?.includes('staging')
? 'did:web:api.staging.bsky.dev'
: 'did:web:api.bsky.app'
const logger = Logger.create(Logger.Context.Notifications) const logger = Logger.create(Logger.Context.Notifications)
async function registerPushToken( async function registerPushToken(
agent: BskyAgent, agent: AtpAgent,
account: SessionAccount, account: SessionAccount,
token: Notifications.DevicePushToken, token: Notifications.DevicePushToken,
) { ) {
try { try {
await agent.api.app.bsky.notification.registerPush({ await agent.app.bsky.notification.registerPush({
serviceDid: SERVICE_DID(account.service), serviceDid: account.service?.includes('staging')
platform: devicePlatform, ? 'did:web:api.staging.bsky.dev'
: 'did:web:api.bsky.app',
platform: Platform.select({
ios: 'ios',
android: 'android',
default: 'web',
}),
token: token.data, token: token.data,
appId: 'xyz.blueskyweb.app', appId: 'xyz.blueskyweb.app',
}) })
logger.debug('Notifications: Sent push token (init)', {
logger.debug(`registerPushToken`, {
tokenType: token.type, tokenType: token.type,
token: token.data, token: token.data,
}) })
} catch (error) { } catch (error) {
logger.error('Notifications: Failed to set push token', {message: error}) logger.error(`registerPushToken: failed`, {safeMessage: error})
} }
} }
@@ -50,14 +52,16 @@ export function useNotificationsRegistration() {
const {currentAccount} = useSession() const {currentAccount} = useSession()
React.useEffect(() => { React.useEffect(() => {
if (!currentAccount) { if (!currentAccount) return
return
}
// HACK - see https://github.com/bluesky-social/social-app/pull/4467 /**
// An apparent regression in expo-notifications causes `addPushTokenListener` to not fire on Android whenever the * HACK — An apparent regression in expo-notifications causes
// token changes by calling `getPushToken()`. This is a workaround to ensure we register the token once it is * `addPushTokenListener` to not fire on Android whenever the token changes
// generated on Android. * by calling `getPushToken()`. This is a workaround to ensure we register
* the token once it is generated on Android.
*
* @see https://github.com/bluesky-social/social-app/pull/4467
*/
if (isAndroid) { if (isAndroid) {
;(async () => { ;(async () => {
const token = await getPushToken() const token = await getPushToken()
@@ -71,8 +75,11 @@ export function useNotificationsRegistration() {
getPushToken() 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. * 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.
*/
const subscription = Notifications.addPushTokenListener(async newToken => { const subscription = Notifications.addPushTokenListener(async newToken => {
registerPushToken(agent, currentAccount, newToken) registerPushToken(agent, currentAccount, newToken)
}) })
@@ -107,24 +114,37 @@ export function useRequestNotificationsPermission() {
} }
const res = await Notifications.requestPermissionsAsync() const res = await Notifications.requestPermissionsAsync()
logEvent('notifications:request', { logger.metric(`notifications:request`, {
context: context, context: context,
status: res.status, status: res.status,
}) })
if (res.granted) { if (res.granted) {
// This will fire a pushTokenEvent, which will handle registration of the token /*
* This will fire a pushTokenEvent, which will handle registration of the
* token
*/
const token = await getPushToken(true) const token = await getPushToken(true)
// Same hack as above. We cannot rely on the `addPushTokenListener` to fire on Android due to an Expo bug, so we /**
// will manually register it here. Note that this will occur only: * Same hack as above. We cannot rely on the `addPushTokenListener` to
// 1. right after the user signs in, leading to no `currentAccount` account being available - this will be instead * fire on Android due to an Expo bug, so we will manually register it
// picked up from the useEffect above on `currentAccount` change * here. Note that this will occur only:
// 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 * 1) Right after the user signs in, leading to no `currentAccount`
// be available in this case, so the registration will succeed. * account being available - this will be instead picked up from the
// We should remove this once expo-notifications (and possibly FCMv1) is fixed and the `addPushTokenListener` is * useEffect above on `currentAccount` change
// working again. See https://github.com/expo/expo/issues/28656 *
* 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.
*
* @see https://github.com/expo/expo/issues/28656
*/
if (isAndroid && currentAccount && token) { if (isAndroid && currentAccount && token) {
registerPushToken(agent, currentAccount, token) registerPushToken(agent, currentAccount, token)
} }
-1
View File
@@ -3,7 +3,6 @@ import {Platform} from 'react-native'
export const isIOS = Platform.OS === 'ios' export const isIOS = Platform.OS === 'ios'
export const isAndroid = Platform.OS === 'android' export const isAndroid = Platform.OS === 'android'
export const isNative = isIOS || isAndroid export const isNative = isIOS || isAndroid
export const devicePlatform = isIOS ? 'ios' : isAndroid ? 'android' : 'web'
export const isWeb = !isNative export const isWeb = !isNative
export const isMobileWebMediaQuery = 'only screen and (max-width: 1300px)' export const isMobileWebMediaQuery = 'only screen and (max-width: 1300px)'
export const isMobileWeb = export const isMobileWeb =