diff --git a/lib/core/routes/app_routes.dart b/lib/core/routes/app_routes.dart index 455e70d..f76ef51 100644 --- a/lib/core/routes/app_routes.dart +++ b/lib/core/routes/app_routes.dart @@ -50,11 +50,14 @@ class LoginScreenRoute extends GoRouteData with _$LoginScreenRoute { // splash route - outside the shell @TypedGoRoute(path: '/splash') class SplashScreenRoute extends GoRouteData with _$SplashScreenRoute { - const SplashScreenRoute(); + final bool? navigateToNotification; + const SplashScreenRoute({this.navigateToNotification}); @override Widget build(BuildContext context, GoRouterState state) { - return const SplashScreen(); + return SplashScreen( + navigateToNotification: navigateToNotification ?? false, + ); } } diff --git a/lib/core/routes/app_routes.g.dart b/lib/core/routes/app_routes.g.dart index 14669d1..d3d2e7c 100644 --- a/lib/core/routes/app_routes.g.dart +++ b/lib/core/routes/app_routes.g.dart @@ -51,11 +51,24 @@ RouteBase get $splashScreenRoute => GoRouteData.$route( ); mixin _$SplashScreenRoute on GoRouteData { - static SplashScreenRoute _fromState(GoRouterState state) => - const SplashScreenRoute(); + static SplashScreenRoute _fromState(GoRouterState state) => SplashScreenRoute( + navigateToNotification: _$convertMapValue( + 'navigate-to-notification', + state.uri.queryParameters, + _$boolConverter, + ), + ); + + SplashScreenRoute get _self => this as SplashScreenRoute; @override - String get location => GoRouteData.$location('/splash'); + String get location => GoRouteData.$location( + '/splash', + queryParams: { + if (_self.navigateToNotification != null) + 'navigate-to-notification': _self.navigateToNotification!.toString(), + }, + ); @override void go(BuildContext context) => context.go(location); @@ -71,6 +84,26 @@ mixin _$SplashScreenRoute on GoRouteData { void replace(BuildContext context) => context.replace(location); } +T? _$convertMapValue( + String key, + Map map, + T? Function(String) converter, +) { + final value = map[key]; + return value == null ? null : converter(value); +} + +bool _$boolConverter(String value) { + switch (value) { + case 'true': + return true; + case 'false': + return false; + default: + throw UnsupportedError('Cannot convert "$value" into a bool.'); + } +} + RouteBase get $appShellRouteData => StatefulShellRouteData.$route( parentNavigatorKey: AppShellRouteData.$parentNavigatorKey, factory: $AppShellRouteDataExtension._fromState, diff --git a/lib/main.dart b/lib/main.dart index 4107ecd..1fb7fc2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ // main.dart import 'dart:ui'; +import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -53,6 +54,23 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { + @override + void initState() { + super.initState(); + _handleInitialMessage(); + } + + Future _handleInitialMessage() async { + final message = await FirebaseMessaging.instance.getInitialMessage(); + if (message != null) { + WidgetsBinding.instance.addPostFrameCallback((_) { + appRouter.go( + const SplashScreenRoute(navigateToNotification: true).location, + ); + }); + } + } + @override void didChangeDependencies() { super.didChangeDependencies(); diff --git a/lib/views/splash/pages/m_splash_page.dart b/lib/views/splash/pages/m_splash_page.dart index 109b120..a030b04 100644 --- a/lib/views/splash/pages/m_splash_page.dart +++ b/lib/views/splash/pages/m_splash_page.dart @@ -6,7 +6,8 @@ import 'package:tm_app/core/utils/size_config.dart'; import 'package:tm_app/view_models/auth_view_model.dart'; class MobileSplashPage extends StatefulWidget { - const MobileSplashPage({super.key}); + const MobileSplashPage({required this.navigateToNotification, super.key}); + final bool navigateToNotification; @override State createState() => _MobileSplashPageState(); @@ -27,7 +28,14 @@ class _MobileSplashPageState extends State { void _viewModelListener() { if (viewModel.isLoggedIn) { WidgetsBinding.instance.addPostFrameCallback((_) { - Router.neglect(context, () => const HomeRouteData().go(context)); + if (widget.navigateToNotification) { + Router.neglect( + context, + () => const NotificationRouteData().go(context), + ); + } else { + Router.neglect(context, () => const HomeRouteData().go(context)); + } }); } else { WidgetsBinding.instance.addPostFrameCallback((_) { diff --git a/lib/views/splash/pages/splash_screen.dart b/lib/views/splash/pages/splash_screen.dart index 64ed6c4..22a1ced 100644 --- a/lib/views/splash/pages/splash_screen.dart +++ b/lib/views/splash/pages/splash_screen.dart @@ -6,15 +6,16 @@ import 'package:tm_app/views/splash/pages/m_splash_page.dart'; import 'package:tm_app/views/splash/pages/t_splash_page.dart'; class SplashScreen extends StatelessWidget { - const SplashScreen({super.key}); + const SplashScreen({required this.navigateToNotification, super.key}); + final bool navigateToNotification; @override Widget build(BuildContext context) { SizeConfig.init(context); - return const ResponsiveBuilder( - mobile: MobileSplashPage(), - tablet: TabletSplashPage(), - desktop: DesktopSplashPage(), + return ResponsiveBuilder( + mobile: MobileSplashPage(navigateToNotification: navigateToNotification), + tablet: const TabletSplashPage(), + desktop: const DesktopSplashPage(), ); } }