Skip to content

fix(app): navigate from FCM background/terminated notification taps (#5126) - #11144

Merged
kodjima33 merged 4 commits into
BasedHardware:mainfrom
aryanorastar:fix/5126-notification-tap-nav
Aug 6, 2026
Merged

kodjima33 merged 4 commits into
BasedHardware:mainfrom
aryanorastar:fix/5126-notification-tap-nav

Conversation

@aryanorastar

@aryanorastar aryanorastar commented Aug 5, 2026 •

Copy link
Copy Markdown
Collaborator

What changed and why

Tapping an Omi/plugin chat push from background or terminated state opened the last screen because listenForMessages() only handled foreground onMessage. Backend already sends navigate_to (e.g. /chat/omi). Register onMessageOpenedApp + getInitialMessage, and expose NotificationUtil.handleNavigateTo. Fixes #5126.

Terminated cold-start harden: getInitialMessage can resolve before runApp attaches globalNavigatorKey, so a bare currentState?.pushReplacement silently no-ops. _handleAppLinkOrDeepLink now polls via waitUntilNonNull until the navigator is ready (same race class as app_links in app_shell / #4763), then pushes.

Resurrects closed unmerged #5127 with Gemini’s type-safe helper feedback applied.

Product invariants affected

none

How it was verified

cd app && flutter test test/unit/notification_fcm_navigate_to_test.dart
# 7 passed

Could not exercise a live FCM tap from background/terminated on a device in this environment; wiring matches FlutterFire’s documented handlers and the prior #5127 approach. Navigator-wait helper is unit-tested for immediate / delayed / timeout cases.

Tests

app/test/unit/notification_fcm_navigate_to_test.dart — navigateToFromFcmData accepts non-empty String routes and rejects missing/empty/non-String values; waitUntilNonNull covers cold-start readiness polling.

Failure class (fixes)

Failure-Class: none

Scoped cleanups (optional)

n/a

…asedHardware#5126)

Register onMessageOpenedApp and getInitialMessage so push taps honor
navigate_to instead of leaving the user on the last screen. Resurrects
closed BasedHardware#5127 with a shared String-safe navigate_to parser.

Co-authored-by: Cursor <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

…dware#5126)

getInitialMessage can resolve before runApp attaches globalNavigatorKey,
so pushReplacement previously no-op'd on terminated taps. Poll until the
navigator is ready (same race class as app_links BasedHardware#4763).

Co-authored-by: Cursor <[email protected]>
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Follow-up @cf6340f7: wait for globalNavigatorKey before FCM/AwesomeNotifications navigate_to push so terminated cold starts (getInitialMessage resolving before runApp) no longer silently no-op. Same race class as app_links #4763. Unit tests for waitUntilNonNull added (7 total in the file).

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found and verified against the latest diff

Confidence score: 3/5

  • In app/lib/services/notifications/notification_service_fcm.dart, a terminated-launch path can race because getInitialMessage() may resolve while _init() is still running before runApp() in main.dart, so the first notification/deep-link intent can be missed or handled inconsistently on cold start; make initialization order deterministic (or buffer initial-message handling until init completes) to de-risk startup behavior.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="app/lib/services/notifications/notification_service_fcm.dart">

<violation number="1" location="app/lib/services/notifications/notification_service_fcm.dart:278">
P2: In a terminated-state launch, `getInitialMessage()` resolves while `_init()` is still awaiting work before `runApp()` (see main.dart: `await NotificationService.instance.initialize()` runs well before `runApp(const MyApp())`). The `.then(handleNotificationTap)` callback therefore runs before the widget tree is built, so `_handleAppLinkOrDeepLink`'s `globalNavigatorKey.currentState?.pushReplacement(...)` sees `currentState == null` and silently does nothing — dropping the very tap this change is meant to navigate. This makes the #5126 fix timing-dependent (works only if `getInitialMessage` happens to resolve after the navigator attaches). Consider deferring the navigation until the navigator is available (e.g. wait for the first frame / a navigator-ready signal) or re-checking once the app is fully up before navigating.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

FirebaseMessaging.onMessageOpenedApp.listen(handleNotificationTap);

// Terminated: app was killed and opened via notification tap (#5126).
FirebaseMessaging.instance.getInitialMessage().then(handleNotificationTap);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: In a terminated-state launch, getInitialMessage() resolves while _init() is still awaiting work before runApp() (see main.dart: await NotificationService.instance.initialize() runs well before runApp(const MyApp())). The .then(handleNotificationTap) callback therefore runs before the widget tree is built, so _handleAppLinkOrDeepLink's globalNavigatorKey.currentState?.pushReplacement(...) sees currentState == null and silently does nothing — dropping the very tap this change is meant to navigate. This makes the #5126 fix timing-dependent (works only if getInitialMessage happens to resolve after the navigator attaches). Consider deferring the navigation until the navigator is available (e.g. wait for the first frame / a navigator-ready signal) or re-checking once the app is fully up before navigating.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/lib/services/notifications/notification_service_fcm.dart, line 278:

<comment>In a terminated-state launch, `getInitialMessage()` resolves while `_init()` is still awaiting work before `runApp()` (see main.dart: `await NotificationService.instance.initialize()` runs well before `runApp(const MyApp())`). The `.then(handleNotificationTap)` callback therefore runs before the widget tree is built, so `_handleAppLinkOrDeepLink`'s `globalNavigatorKey.currentState?.pushReplacement(...)` sees `currentState == null` and silently does nothing — dropping the very tap this change is meant to navigate. This makes the #5126 fix timing-dependent (works only if `getInitialMessage` happens to resolve after the navigator attaches). Consider deferring the navigation until the navigator is available (e.g. wait for the first frame / a navigator-ready signal) or re-checking once the app is fully up before navigating.</comment>

<file context>
@@ -261,6 +262,20 @@ class _FCMNotificationService implements NotificationInterface {
+    FirebaseMessaging.onMessageOpenedApp.listen(handleNotificationTap);
+
+    // Terminated: app was killed and opened via notification tap (#5126).
+    FirebaseMessaging.instance.getInitialMessage().then(handleNotificationTap);
   }
 
</file context>

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already addressed in cf6340f7 (before this review comment landed).

_handleAppLinkOrDeepLink now waits for the navigator before pushing:

final navigator = await waitUntilNonNull(() => globalNavigatorKey.currentState);
if (navigator == null) {
  Logger.debug('Navigator unavailable; dropping navigate_to=$navigateTo');
  return;
}
navigator.pushReplacement(...);

So terminated cold starts where getInitialMessage resolves before runApp no longer no-op via currentState?.pushReplacement. Follow-up 730adc8c only clears the analyzer ratchet (const test maps).

…Hardware#5126)

Non-const map literals tripped prefer_const_literals (baseline 0). Drop
the redundant foundation import that also added an unnecessary_import.

Co-authored-by: Cursor <[email protected]>
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

CI: 730adc8c — const on the five FCM navigate_to test map literals so prefer_const_literals_to_create_immutables (baseline 0) no longer trips the analyzer ratchet. Also dropped a redundant foundation.dart import from the earlier harden commit.

cubic P2: already covered by waitUntilNonNull in cf6340f7 — replied on the review thread.

…re#5126)

Detect Hermetic Backend Scope hit the 2m job timeout (cancelled); Merge Gate
fail-closed on SCOPE_RESULT=cancelled. App-only PR — no backend scope change.

Co-authored-by: Cursor <[email protected]>
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

CI note: Backend Hermetic Merge Gate red is not from this PR’s app changes.

Detect Hermetic Backend Scope hit the 2m job timeout and was cancelled → Merge Gate fail-closed on SCOPE_RESULT=cancelled (E2E jobs correctly skipped). No admin rights to gh run rerun; pushed an empty commit to retrigger.

Cubic P2 remains covered by waitUntilNonNull in cf6340f7.

@Git-on-my-level Git-on-my-level added flutter flutter work positive-signal Automation verified a genuine fix/quality contribution labels Aug 5, 2026
@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @aryanorastar — solid fix for #5126. I reviewed the full diff against main and the cold-start reasoning holds up.

What I verified

  • FCM wiring: onMessageOpenedApp.listen + getInitialMessage().then correctly route navigate_to taps from background/terminated states into NotificationUtil.handleNavigateTo, matching FlutterFire's documented handlers.
  • Cold-start race (the cubic P2): confirmed the ordering — main() does await _init() (which registers getInitialMessage().then(...)) before runApp(MyApp()), and globalNavigatorKey.currentState only becomes non-null once MaterialApp(navigatorKey: ...) builds. So the old currentState?.pushReplacement(...) genuinely no-ops on a terminated cold start. The new await waitUntilNonNull(() => globalNavigatorKey.currentState) before pushReplacement resolves this — and because handleNavigateTo wraps the call in unawaited(...), the polling survives until the navigator attaches. Good catch carrying this over from fix: deep link race condition — fires before providers initialized on cold start #4763/fix(notifications): handle FCM background/terminated notification tap navigation #5127.
  • Type safety: navigateToFromFcmData correctly rejects missing/empty/non-String navigate_to values; the 7 new unit tests cover the helper matrix and the waitUntilNonNull immediate/delayed/timeout paths.
  • Checks: Dart Analyze & Tests, Android Compile Smoke, Hygiene, and Formatting all green. No dependency, auth, secrets, or workflow surfaces touched.

Minor follow-up (non-blocking)

  • waitUntilNonNull polls every 16ms for up to 15s. It's pragmatic and tested, but a navigator-ready Completer fired from MyApp.initState (or endOfFrame) would be more idiomatic than busy-polling. Happy to leave as-is for this fix.

Marking as a positive signal — leaving for a human maintainer to merge. Appreciate you resurrecting #5127 with the type-safety feedback applied.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@aryanorastar

Copy link
Copy Markdown
Collaborator Author

@Git-on-my-level Thanks for the positive signal and for verifying the cold-start race against main/_init() ordering.

Agreed the Completer/endOfFrame navigator-ready signal is nicer than 16ms polling — happy to leave the pragmatic waitUntilNonNull for this #5126 fix and revisit if we generalize navigator-ready across FCM + app_links.

No further author code changes pending; leaving APPROVE/merge to a human maintainer.

@aryanorastar

Copy link
Copy Markdown
Collaborator Author

@Git-on-my-level need human response

Head @9a98a424 is fully green (Dart Analyze, Android Compile, Preflight). Cubic P2 already covered by waitUntilNonNull; your positive-signal pass verified the cold-start race against main/_init() ordering. Completer follow-up left as non-blocking.

Merge still blocked solely on write-access APPROVE. Could a human maintainer APPROVE/merge when you have a moment? No further author work pending.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registers onMessageOpenedApp/getInitialMessage and waits for the navigator on cold start so notification taps route correctly.

@kodjima33
kodjima33 merged commit fc23a2c into BasedHardware:main Aug 6, 2026
30 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flutter flutter work positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Tapping Omi response notifications doesn't navigate to the correct conversation

3 participants