Skip to content

[go_router] Navigate to route when user opens the app via a push notification #136250

@JGeek00

Description

@JGeek00

Is there an existing issue for this?

Steps to reproduce

I'm trying to navigate to a route when the user clicks on a notification. I'm handling push notifications with FirebaseMessaging, and the notification system works fine. Currently the only step that is remaining to have the whole notification process working is to navigate to a route when the user opens the app via a notification.
I tried doing this on the main.dart initState

  void initState() {
    _pushNotificationService.initialize();

    super.initState();

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print(message.data);  // message.data contains the route to navigate
      WidgetsBinding.instance.addPostFrameCallback((_) {
        GoRouter.of(context).go("/route1");
      });
    });
  }

But it always throws this error: No GoRouter found in context

Actual results

I receive this error: No GoRouter found in context

Logs

Logs
<!-- Paste your logs here -->

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.13.5, on Microsoft Windows [Version 10.0.19044.3448], locale es-ES)
    • Flutter version 3.13.5 on channel stable at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 12fccda598 (3 weeks ago), 2023-09-19 13:56:11 -0700
    • Engine revision bd986c5ed2
    • Dart version 3.1.2
    • DevTools version 2.25.0

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\jgilsanz\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[!] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.15)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
    • Visual Studio Build Tools 2019 version 16.11.32510.428
    • Windows 10 SDK version 10.0.19041.0
    X The current Visual Studio installation is incomplete. Please reinstall Visual Studio.

[√] Android Studio (version 2022.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)

[√] VS Code (version 1.83.0)
    • VS Code at C:\Users\jgilsanz\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.74.0

[√] Connected device (4 available)
    • sdk gphone x86 64 (mobile) • emulator-5554 • android-x64    • Android 13 (API 33) (emulator)
    • Windows (desktop)          • windows       • windows-x64    • Microsoft Windows [Version 10.0.19044.3448]
    • Chrome (web)               • chrome        • web-javascript • Google Chrome 117.0.5938.149
    • Edge (web)                 • edge          • web-javascript • Microsoft Edge 117.0.2045.31

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: solvedIssue is closed as solved

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions