fix(notifications): handle missing UnifiedPush distributor command on Android

This commit is contained in:
Erwan Leboucher committed 2026-07-18 13:51:45 +02:00
1 parent 3a8bd64d1a
commit 86403ae649
3 files changed
+19 -9

No files matched your search

@@ -62,6 +62,7 @@ import {
tryEnableUnifiedPush,
type UnifiedPushTransportConfigInput,
} from './UnifiedPushNotifications';
import { normalizeErrorMessage } from './UnifiedPushTransport';
import {
ensureUnifiedPushDistributorSelection,
loadUnifiedPushDistributorState,
@@ -564,7 +565,7 @@ function BackgroundPushNotificationSetting() {
})
.catch((caughtError) => {
if (!active) return;
setError(caughtError instanceof Error ? caughtError.message : String(caughtError));
setError(normalizeErrorMessage(caughtError));
})
.finally(() => {
if (active) setIsLoading(false);
@@ -770,7 +771,7 @@ function BackgroundPushNotificationSetting() {
}
setBackgroundPushEnabled(wantsPush);
} catch (caughtError) {
setError(caughtError instanceof Error ? caughtError.message : String(caughtError));
setError(normalizeErrorMessage(caughtError));
} finally {
setIsLoading(false);
}
@@ -795,7 +796,7 @@ function BackgroundPushNotificationSetting() {
}
setPushTransportMode(nextMode);
} catch (caughtError) {
setError(caughtError instanceof Error ? caughtError.message : String(caughtError));
setError(normalizeErrorMessage(caughtError));
} finally {
setIsLoading(false);
}
@@ -827,7 +828,7 @@ function BackgroundPushNotificationSetting() {
setSelectedDistributor(distributor);
updatePushTransportOverride({ unifiedPushDistributor: distributor });
} catch (caughtError) {
setError(caughtError instanceof Error ? caughtError.message : String(caughtError));
setError(normalizeErrorMessage(caughtError));
} finally {
setIsLoading(false);
}
@@ -186,11 +186,13 @@ describe('UnifiedPush distributor state helpers', () => {
);
});
it('surfaces backend errors while loading distributor state', async () => {
it('returns empty distributors when the backend is unavailable', async () => {
localStorage.removeItem('unifiedpush_distributor');
unifiedPushApi.listDistributors.mockRejectedValue(new Error('backend unavailable'));
await expect(loadUnifiedPushDistributorState()).rejects.toThrow('backend unavailable');
const result = await loadUnifiedPushDistributorState();
expect(result.distributors).toEqual([]);
expect(result.selectedDistributor).toBe('');
});
it('restores the previous distributor when a switch registration fails', async () => {
@@ -48,7 +48,7 @@ export type UnifiedPushRegistrationResult =
const DISTRIBUTUTOR_STORAGE_KEY = 'unifiedpush_distributor';
function normalizeErrorMessage(error: unknown): string {
export function normalizeErrorMessage(error: unknown): string {
if (error instanceof Error) return error.message;
if (typeof error === 'string') return error;
if (error && typeof error === 'object') {
@@ -125,11 +125,18 @@ export async function saveUnifiedPushDistributor(distributor: string): Promise<v
}
export async function loadUnifiedPushDistributorState(): Promise<UnifiedPushDistributorState> {
const [distributorResult, distributors] = await Promise.all([
const [distributorResult, distributorsResult] = await Promise.allSettled([
getUnifiedPushDistributor(),
getUnifiedPushDistributors(),
]);
const savedDistributor = distributorResult.distributor;
const savedDistributor =
distributorsResult.status === 'fulfilled'
? distributorResult.status === 'fulfilled'
? distributorResult.value.distributor
: ''
: '';
const distributors = distributorsResult.status === 'fulfilled' ? distributorsResult.value : [];
if (savedDistributor && distributors.includes(savedDistributor)) {
return { distributors, selectedDistributor: savedDistributor };