-
Notifications
You must be signed in to change notification settings - Fork 14
fix(native): route push notification taps inside the app #2470
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
3 commits
Select commit
Hold shift + click to select a range
4fc04be
fix(native): route push notification taps inside the app
innolope-dev cf4ba72
fix(native): harden deep-link mapping against malformed and reserved …
innolope-dev 86e0766
fix(native): buffer cold-start push clicks, open external links in br…
innolope-dev 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| 'use client' | ||
|
|
||
| import { useEffect } from 'react' | ||
| import { useRouter, useSearchParams } from 'next/navigation' | ||
| import { useQuery } from '@tanstack/react-query' | ||
| import PageContainer from '@/components/0_Bruddle/PageContainer' | ||
| import NavHeader from '@/components/Global/NavHeader' | ||
| import PeanutLoading from '@/components/Global/PeanutLoading' | ||
| import { TransactionDetailsReceipt } from '@/components/TransactionDetails/TransactionDetailsReceipt' | ||
| import { mapTransactionDataForDrawer } from '@/components/TransactionDetails/transactionTransformer' | ||
| import { resolveReceiptKind } from '@/components/TransactionDetails/strategies/registry' | ||
| import { TRANSACTIONS } from '@/constants/query.consts' | ||
| import { apiFetch } from '@/utils/api-fetch' | ||
| import { completeHistoryEntry, isFinalState, type HistoryEntry } from '@/utils/history.utils' | ||
|
|
||
| // Client twin of the web /receipt/[entryId] page. That one is a server component | ||
| // and is stripped from the static export (scripts/native-build.js), so a receipt | ||
| // deep link — the most common push destination — had nothing to render natively. | ||
| // deepLinkToNativePath maps /receipt/<id>?kind=X onto this route's query params. | ||
| export default function NativeReceiptPage() { | ||
| const router = useRouter() | ||
| const searchParams = useSearchParams() | ||
| const entryId = searchParams.get('id') | ||
| const kind = resolveReceiptKind(searchParams.get('kind') ?? undefined, searchParams.get('t') ?? undefined) | ||
|
|
||
| const { | ||
| data: entry, | ||
| isLoading, | ||
| isError, | ||
| } = useQuery({ | ||
| queryKey: [TRANSACTIONS, 'entry', entryId, kind], | ||
| enabled: Boolean(entryId && kind), | ||
| queryFn: async (): Promise<HistoryEntry> => { | ||
| const response = await apiFetch(`/history/${encodeURIComponent(entryId!)}?kind=${kind}`) | ||
| if (!response.ok) throw new Error(`Failed to fetch history entry: ${response.status}`) | ||
| return completeHistoryEntry(await response.json()) | ||
| }, | ||
| // A pending transaction can still settle while the receipt is open. | ||
| refetchInterval: (query) => (query.state.data && !isFinalState(query.state.data) ? 15_000 : false), | ||
| }) | ||
|
|
||
| // `isError` also trips on a failed background poll, so gate the bail-out on | ||
| // having no data at all — a flaky refetch must not yank the user off a | ||
| // receipt they're watching settle. | ||
| const isUnrecoverable = !entryId || !kind || (isError && !entry) | ||
|
|
||
| useEffect(() => { | ||
| if (isUnrecoverable) router.replace('/home') | ||
| }, [isUnrecoverable, router]) | ||
|
|
||
| if (isUnrecoverable) return null | ||
|
|
||
| return ( | ||
| <PageContainer className="flex min-h-[100dvh] flex-col items-center justify-center p-6"> | ||
| <div className="md:hidden"> | ||
| <NavHeader title="Receipt" /> | ||
| </div> | ||
| <div className="flex flex-1 flex-col items-center justify-center"> | ||
| {isLoading || !entry ? ( | ||
| <PeanutLoading /> | ||
| ) : ( | ||
| <TransactionDetailsReceipt | ||
| className="w-full" | ||
| transaction={mapTransactionDataForDrawer(entry).transactionDetails} | ||
| /> | ||
| )} | ||
| </div> | ||
| </PageContainer> | ||
| ) | ||
| } | ||
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,80 @@ | ||
| import type { NotificationClickInfo } from './types' | ||
|
|
||
| // virtual: the plugin ships ESM-only exports jest's resolver can't load; | ||
| // the adapter only ever runs in native builds where webpack resolves it. | ||
| jest.mock( | ||
| '@onesignal/capacitor-plugin', | ||
| () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| initialize: jest.fn().mockResolvedValue(undefined), | ||
| login: jest.fn(), | ||
| logout: jest.fn(), | ||
| Notifications: { | ||
| addEventListener: jest.fn(), | ||
| hasPermission: jest.fn().mockResolvedValue(true), | ||
| canRequestPermission: jest.fn().mockResolvedValue(true), | ||
| requestPermission: jest.fn(), | ||
| }, | ||
| User: { | ||
| pushSubscription: { | ||
| addEventListener: jest.fn(), | ||
| getOptedInAsync: jest.fn().mockResolvedValue(true), | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| { virtual: true } | ||
| ) | ||
|
|
||
| import OneSignal from '@onesignal/capacitor-plugin' | ||
| import { nativeOneSignalAdapter } from './native.adapter' | ||
|
|
||
| function getClickCallback(): (event: unknown) => void { | ||
| const call = (OneSignal.Notifications.addEventListener as jest.Mock).mock.calls.find(([name]) => name === 'click') | ||
| expect(call).toBeDefined() | ||
| return call![1] | ||
| } | ||
|
|
||
| describe('nativeOneSignalAdapter cold-start click buffering', () => { | ||
| beforeAll(async () => { | ||
| process.env.NEXT_PUBLIC_ONESIGNAL_APP_ID = 'test-app-id' | ||
| await nativeOneSignalAdapter.init() | ||
| }) | ||
|
|
||
| // The Capacitor bridge retains a cold-start click only until the first JS | ||
| // listener attaches — which init() does. If the event arrives before | ||
| // useNativePlugins has registered its routing callback, the adapter must | ||
| // hold it and replay it, or the tap is silently dropped. | ||
| it('replays a click that fired before any listener registered', () => { | ||
| const fireClick = getClickCallback() | ||
| fireClick({ | ||
| notification: { | ||
| launchURL: 'https://peanut.me/receipt/intent-1?kind=ONRAMP', | ||
| additionalData: { deepLink: '/receipt/intent-1?kind=ONRAMP' }, | ||
| }, | ||
| }) | ||
|
|
||
| const seen: NotificationClickInfo[] = [] | ||
| const off = nativeOneSignalAdapter.onNotificationClick((info) => seen.push(info)) | ||
| expect(seen).toEqual([ | ||
| { | ||
| deepLink: 'https://peanut.me/receipt/intent-1?kind=ONRAMP', | ||
| additionalData: { deepLink: '/receipt/intent-1?kind=ONRAMP' }, | ||
| }, | ||
| ]) | ||
|
|
||
| // one-shot: a second listener must not receive the same tap again | ||
| const seenLater: NotificationClickInfo[] = [] | ||
| const offLater = nativeOneSignalAdapter.onNotificationClick((info) => seenLater.push(info)) | ||
| expect(seenLater).toEqual([]) | ||
|
|
||
| // once listeners exist, clicks are delivered live, not buffered | ||
| fireClick({ result: { url: 'https://peanut.me/rewards' }, notification: { additionalData: {} } }) | ||
| expect(seen).toHaveLength(2) | ||
| expect(seenLater).toHaveLength(1) | ||
|
|
||
| off() | ||
| offLater() | ||
| }) | ||
| }) |
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
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.