-
Notifications
You must be signed in to change notification settings - Fork 9
feat(mobile): add remote push notifications #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79754ff
feat(mobile): add remote push notifications
AprilNEA f536137
test(mobile): cover notification routing and token races
Zerlight cb037f4
fix(mobile): address notification review feedback
Zerlight 874e271
fix(mobile): harden notification revocation
Zerlight bfba11d
fix(mobile): retain enrollment on sign-out failure
Zerlight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { useCloudAccount } from '@mobile/runtime/cloud/account'; | ||
| import { resolveNotificationRoute } from '@mobile/runtime/notification-route'; | ||
| import { activateNotificationSync, syncDevicePushToken } from '@mobile/runtime/notifications'; | ||
| import { useHostRegistryHydrated, useHostRegistryStore } from '@mobile/stores/host-store'; | ||
| import { useSettingsStore } from '@mobile/stores/settings-store'; | ||
| import * as Sentry from '@sentry/react-native'; | ||
| import * as Notifications from 'expo-notifications'; | ||
| import { useRouter } from 'expo-router'; | ||
| import { useEffect, useEffectEvent } from 'react'; | ||
| import { AppState } from 'react-native'; | ||
|
|
||
| export function NotificationObserver(): null { | ||
| const router = useRouter(); | ||
| const account = useCloudAccount(); | ||
| const hydrated = useHostRegistryHydrated(); | ||
| const hosts = useHostRegistryStore((state) => state.hosts); | ||
| const setLastActiveHostId = useHostRegistryStore((state) => state.setLastActiveHostId); | ||
| const enabled = useSettingsStore((state) => state.notificationsEnabled); | ||
| const userId = account.status === 'signed-in' ? account.user.id : null; | ||
|
|
||
| const openNotification = useEffectEvent((response: Notifications.NotificationResponse) => { | ||
| try { | ||
| const target = resolveNotificationRoute(response.notification.request.content.data, hosts); | ||
| if (target?.type === 'session') { | ||
| setLastActiveHostId(target.hostId); | ||
| router.push({ | ||
| pathname: '/session/[sessionId]', | ||
| params: { sessionId: target.sessionId }, | ||
| }); | ||
| } else if (target?.type === 'connect') { | ||
| router.push('/connect'); | ||
| } | ||
| } finally { | ||
| Notifications.clearLastNotificationResponse(); | ||
| } | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (!hydrated) return; | ||
|
|
||
|
Zerlight marked this conversation as resolved.
|
||
| const initial = Notifications.getLastNotificationResponse(); | ||
| if (initial) openNotification(initial); | ||
| const subscription = Notifications.addNotificationResponseReceivedListener(openNotification); | ||
| return () => subscription.remove(); | ||
| }, [hydrated]); | ||
|
|
||
| useEffect(() => { | ||
| if (!enabled || !userId) return; | ||
|
|
||
| const deactivate = activateNotificationSync(userId); | ||
| const sync = (devicePushToken?: Notifications.DevicePushToken) => { | ||
| syncDevicePushToken(userId, devicePushToken).catch((error: unknown) => | ||
| Sentry.captureException(error), | ||
| ); | ||
| }; | ||
|
|
||
| sync(); | ||
| const tokenSubscription = Notifications.addPushTokenListener(sync); | ||
| const appStateSubscription = AppState.addEventListener('change', (state) => { | ||
| if (state === 'active') sync(); | ||
| }); | ||
| return () => { | ||
| deactivate(); | ||
| tokenSubscription.remove(); | ||
| appStateSubscription.remove(); | ||
| }; | ||
| }, [enabled, userId]); | ||
|
|
||
| return null; | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
apps/mobile/src/runtime/__tests__/notification-route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { resolveNotificationRoute } from '../notification-route'; | ||
|
|
||
| describe('resolveNotificationRoute', () => { | ||
| const hosts = [{ id: 'local host', tunnelHostId: 'tunnel-1' }]; | ||
|
|
||
| it('routes known tunnel hosts, falls back for unknown hosts, and rejects invalid data', () => { | ||
| expect( | ||
| resolveNotificationRoute({ tunnelHostId: 'tunnel-1', sessionId: 'session-1' }, hosts), | ||
| ).toEqual({ type: 'session', hostId: 'local host', sessionId: 'session-1' }); | ||
| expect( | ||
| resolveNotificationRoute({ tunnelHostId: 'tunnel-2', sessionId: 'session-2' }, hosts), | ||
| ).toEqual({ type: 'connect' }); | ||
| expect(resolveNotificationRoute({ tunnelHostId: 'tunnel-1' }, hosts)).toBeNull(); | ||
| expect( | ||
| resolveNotificationRoute({ hostId: 'tunnel-1', sessionId: 'session-1' }, hosts), | ||
| ).toBeNull(); | ||
| }); | ||
| }); |
100 changes: 100 additions & 0 deletions
100
apps/mobile/src/runtime/__tests__/notification-token-coordinator.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { noop } from 'foxts/noop'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createNotificationTokenCoordinator } from '../notification-token-coordinator'; | ||
|
|
||
| describe('notification token coordination', () => { | ||
| it('drops a token acquired after notifications are disabled', async () => { | ||
| const coordinator = createNotificationTokenCoordinator(); | ||
| const events: string[] = []; | ||
| let releaseToken!: () => void; | ||
| let markAcquiring!: () => void; | ||
| const tokenGate = new Promise<void>((resolve) => { | ||
| releaseToken = resolve; | ||
| }); | ||
| const acquiring = new Promise<void>((resolve) => { | ||
| markAcquiring = resolve; | ||
| }); | ||
|
|
||
| coordinator.selectUser('user-1'); | ||
| const sync = coordinator.sync( | ||
| 'user-1', | ||
| async () => { | ||
| events.push('acquire'); | ||
| markAcquiring(); | ||
| await tokenGate; | ||
| return 'token'; | ||
| }, | ||
| () => { | ||
| events.push('register'); | ||
| }, | ||
| ); | ||
| await acquiring; | ||
| coordinator.selectUser(null); | ||
| const revoke = coordinator.revoke( | ||
| () => { | ||
| events.push('revoke'); | ||
| }, | ||
| () => { | ||
| events.push('unregister'); | ||
| }, | ||
| ); | ||
| releaseToken(); | ||
|
|
||
| await Promise.all([sync, revoke]); | ||
| expect(events).toEqual(['acquire', 'revoke', 'unregister']); | ||
| }); | ||
|
|
||
| it('runs revocation after an in-flight registration', async () => { | ||
| const coordinator = createNotificationTokenCoordinator(); | ||
| const events: string[] = []; | ||
| let releaseRegistration!: () => void; | ||
| let markRegistering!: () => void; | ||
| const registrationGate = new Promise<void>((resolve) => { | ||
| releaseRegistration = resolve; | ||
| }); | ||
| const registering = new Promise<void>((resolve) => { | ||
| markRegistering = resolve; | ||
| }); | ||
|
|
||
| coordinator.selectUser('user-1'); | ||
| const sync = coordinator.sync( | ||
| 'user-1', | ||
| () => 'token', | ||
| async () => { | ||
| events.push('register:start'); | ||
| markRegistering(); | ||
| await registrationGate; | ||
| events.push('register:end'); | ||
| }, | ||
| ); | ||
| await registering; | ||
| coordinator.selectUser(null); | ||
| const revoke = coordinator.revoke( | ||
| () => { | ||
| events.push('revoke'); | ||
| }, | ||
| () => { | ||
| events.push('unregister'); | ||
| }, | ||
| ); | ||
| releaseRegistration(); | ||
|
|
||
| await Promise.all([sync, revoke]); | ||
| expect(events).toEqual(['register:start', 'register:end', 'revoke', 'unregister']); | ||
| }); | ||
|
|
||
| it('fails revocation only when both the server and native token removal fail', async () => { | ||
| const coordinator = createNotificationTokenCoordinator(); | ||
| coordinator.selectUser(null); | ||
|
|
||
| await expect( | ||
| coordinator.revoke(() => Promise.reject(new Error('server failed')), noop), | ||
| ).resolves.toBeUndefined(); | ||
| await expect( | ||
| coordinator.revoke( | ||
| () => Promise.reject(new Error('server failed')), | ||
| () => Promise.reject(new Error('native failed')), | ||
| ), | ||
| ).rejects.toThrow('server failed'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.