fix(native): the app asks for notifications on iOS too, and again if the question went unanswered

The prompt looked at the app's features once, when the user counted as signed
in. After the sign-in the page loads with the session already there, and on
iOS the bridge had not named its features yet, so the prompt gave up for good.
It now waits for them. It also counted as asked the moment it opened, so a
sheet that went under another one never came back; it counts once answered.
This commit is contained in:
Maurice
2026-11-10 09:26:59 +01:00
parent 6a0ae482d7
commit 3ab4927cb7
2 changed files with 28 additions and 5 deletions
@@ -46,10 +46,12 @@ describe('MPushPrompt', () => {
it('FE-MOB-PUSHPROMPT-001: a moment after signing in the app asks, and turning it on goes the settings card way', async () => {
await renderAndWait();
expect(screen.getByText('Turn on notifications?')).toBeInTheDocument();
expect(window.localStorage.getItem(PUSH_PROMPT_KEY)).not.toBeNull();
// Asked once answered, not once shown.
expect(window.localStorage.getItem(PUSH_PROMPT_KEY)).toBeNull();
fireEvent.click(screen.getByRole('button', { name: 'Turn on' }));
expect(env.toggle).toHaveBeenCalledTimes(1);
expect(window.localStorage.getItem(PUSH_PROMPT_KEY)).not.toBeNull();
expect(screen.queryByText('Turn on notifications?')).not.toBeInTheDocument();
});
@@ -77,6 +79,18 @@ describe('MPushPrompt', () => {
expect(window.localStorage.getItem(PUSH_PROMPT_KEY)).toBeNull();
});
it('FE-MOB-PUSHPROMPT-005: a prompt left unanswered asks again on the next launch', async () => {
const first = render(<MPushPrompt />);
await act(async () => {
await vi.advanceTimersByTimeAsync(PUSH_PROMPT_DELAY_MS);
});
expect(screen.getByText('Turn on notifications?')).toBeInTheDocument();
first.unmount();
await renderAndWait();
expect(screen.getByText('Turn on notifications?')).toBeInTheDocument();
});
it('FE-MOB-PUSHPROMPT-004: signed out, it waits', async () => {
useAuthStore.setState({ isAuthenticated: false } as never);
await renderAndWait();
+13 -4
View File
@@ -2,6 +2,7 @@ import { BellRing } from 'lucide-react';
import React, { useEffect, useState } from 'react';
import { useWebPush } from '../../hooks/useWebPush';
import { useTranslation } from '../../i18n';
import { useNativeCapabilities } from '../../native/capabilities';
import { nativePushActive, nativePushEndpoint } from '../../native/push';
import { pushChannelOffered } from '../../push/webPush';
import { useAuthStore } from '../../store/authStore';
@@ -36,9 +37,14 @@ function rememberAsked(): void {
* server offers push, and only while this device is not signed up yet. Turning
* it on goes the way the settings' push card goes (useWebPush), which brings up
* the system's question.
*
* It counts as asked once the user answers it, not when it opens: a sheet that
* went under another one, or an app closed while it was up, asks again. And it
* waits for the app's capabilities, which can arrive after the sign-in.
*/
export default function MPushPrompt(): React.ReactElement | null {
const signedIn = useAuthStore((s) => s.isAuthenticated);
const { caps } = useNativeCapabilities();
const [open, setOpen] = useState(false);
useEffect(() => {
@@ -46,8 +52,7 @@ export default function MPushPrompt(): React.ReactElement | null {
let cancelled = false;
const timer = window.setTimeout(() => {
void pushChannelOffered().then((offered) => {
if (cancelled || !offered || nativePushEndpoint()) return;
rememberAsked();
if (cancelled || !offered || nativePushEndpoint() || alreadyAsked()) return;
setOpen(true);
});
}, PUSH_PROMPT_DELAY_MS);
@@ -55,9 +60,13 @@ export default function MPushPrompt(): React.ReactElement | null {
cancelled = true;
window.clearTimeout(timer);
};
}, [signedIn]);
}, [signedIn, caps]);
return open ? <PushPromptSheet onClose={() => setOpen(false)} /> : null;
const answered = () => {
rememberAsked();
setOpen(false);
};
return open ? <PushPromptSheet onClose={answered} /> : null;
}
function PushPromptSheet({ onClose }: { onClose: () => void }): React.ReactElement {