How to redirect user when app is terminated state, with Nested Navigation implementation?

Scenario

I am using the firebase_messaging package to handle notifications for my Flutter project. I have a Nested Navigation flow implemented, implementation similar to here.

Problem

I am able to handle notifications when the app is in the foreground or in the background, for nested navigation routes. However, I am facing difficulty handling notifications when the app is in the terminated state. I have tried following the Firebase documentation

Code

In my MaterialApp widget in app.dart, I’ve omitted the routes parameter, and have populated the onGenerateRoute parameter with:

NavigationService.dart

  Route<dynamic> onGenerateRoute(RouteSettings settings) {
    String path = settings.name!;
    Object? arguments = settings.arguments;
    if (settings.arguments == null) {
      Uri uri = Uri.parse(settings.name!);
      path = uri.path;
      arguments = uri.queryParameters;
    }
    if (_appRoutes[path] != null) {
      if (Platform.isIOS) {
        return CupertinoPageRoute(
          settings: settings,
          builder: (_) => _appRoutes[path]!(arguments),
        );
      }
      return MaterialPageRoute(
        settings: settings,
        builder: (_) => _appRoutes[path]!(arguments),
      );
    } else {
      print('route: $path');
      throw SystemException('onGenerateRoute: Route not found');
    }
  }

Each of my nested navigation services also contain their own implementation of onGenerateRoute, which checks against specific routes that they contain.

When I attempt to navigate to a “nested route”, ie. /profile, I get the following error message, even though my ProfileNavigationService contains the route

I/flutter ( 8088): route: /profile
I/flutter ( 8088): onGenerateRoute: Route not found     
I/flutter ( 8088): #0      NavigationService.onGenerateRoute (package:vibefam/routes/navigation_service.dart:359)
I/flutter ( 8088): #1      _WidgetsAppState._onGenerateRoute (package:flutter/src/widgets/app.dart:1482)        
I/flutter ( 8088): #2      NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:4447)

This is how I’ve set up my notification listeners:

  Future<void> _setupNotificationHandler() async {
    // handles interaction when app is terminated
    RemoteMessage? initialMessage =
        await FirebaseMessaging.instance.getInitialMessage();

    if (initialMessage != null) {
      _handleMessage(initialMessage);
    }
    // handles interaction when app is in the background
    FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
  }

  void _handleMessage(RemoteMessage message) {
    String? parsedRoute = NotificationUtil.getRoute(message);
    if (parsedRoute == null) {
      return;
    }

    Uri uri = Uri.parse(parsedRoute);
    String path = uri.path;
    Map<String, dynamic> params = uri.queryParameters;
    if (!NestedRouteHandler.hasNestedRoute(path)) {
      NavigationService.of(context).navigateTo(path, arguments: params);
      return;
    }
    NestedRouteHandler.navigate(context, path, params);
  }

  @override
  void initState() {
    super.initState();
    _setupNotificationHandler();
  }

New contributor

skythefire is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật