From b793cb2a4bc4ecaf088a215d07bc00b1aa52f62b Mon Sep 17 00:00:00 2001 From: "Mr.Jebelli" Date: Sun, 21 Jun 2026 15:55:51 +0330 Subject: [PATCH] replay pending notification tap after login --- lib/providers/user.dart | 8 ++++++++ .../notification/notification_service.dart | 18 ++++++++++++++++++ lib/views/splash/splash.dart | 12 ++++++++++++ 3 files changed, 38 insertions(+) diff --git a/lib/providers/user.dart b/lib/providers/user.dart index c6805ea..218fcc9 100644 --- a/lib/providers/user.dart +++ b/lib/providers/user.dart @@ -14,6 +14,7 @@ import 'package:didvan/services/auth/token_storage.dart'; import 'package:didvan/services/network/request.dart'; import 'package:didvan/services/network/request_helper.dart'; import 'package:didvan/services/notification/firebase_api.dart'; +import 'package:didvan/services/notification/notification_service.dart'; import 'package:didvan/services/storage/storage.dart'; import 'package:didvan/utils/action_sheet.dart'; @@ -188,6 +189,13 @@ class UserProvider extends CoreProvier { // ignore: discarded_futures fetchWelcomeMessage(); + // If the user reached us via a notification tap while logged out, + // splash stashed the payload. Now that we're authenticated, replay + // it so they land on the right detail screen instead of staying on + // the home page. + // ignore: discarded_futures + NotificationService.consumePendingNotificationTap(); + return true; } catch (e) { print("UserProvider: Exception processing user info: $e"); diff --git a/lib/services/notification/notification_service.dart b/lib/services/notification/notification_service.dart index 4f299fb..93659e8 100644 --- a/lib/services/notification/notification_service.dart +++ b/lib/services/notification/notification_service.dart @@ -72,6 +72,24 @@ class NotificationService { } } + /// Save a notification payload into cross-isolate storage so it can be + /// replayed later (e.g. after the user logs in). Used by splash when the + /// user tapped a notification while logged out — we don't want to drop + /// the destination just because they need to sign in first. + static Future stashPendingNotificationPayload( + NotificationMessage data) async { + try { + await HomeWidget.saveWidgetData( + pendingNotificationPayloadKey, + jsonEncode(data.toJson()), + ); + } catch (e) { + if (kDebugMode) { + print('stashPendingNotificationPayload failed: $e'); + } + } + } + /// Read a pending notification payload (if any) from cross-isolate /// storage and hand it off to [HomeWidgetRepository.data] without /// navigating. Use this during splash where you want the existing splash diff --git a/lib/views/splash/splash.dart b/lib/views/splash/splash.dart index 32dab6b..44c54c4 100644 --- a/lib/views/splash/splash.dart +++ b/lib/views/splash/splash.dart @@ -378,6 +378,18 @@ class _SplashState extends State with SingleTickerProviderStateMixin { return; } + // User tapped a notification but isn't logged in — stash the payload + // back into cross-isolate storage so we can replay it after a + // successful login (see UserProvider.getUserInfo). + if (token == null && HomeWidgetRepository.data != null) { + try { + await NotificationService.stashPendingNotificationPayload( + HomeWidgetRepository.data!, + ); + } catch (_) {/* best effort */} + HomeWidgetRepository.data = null; + } + Navigator.of(context).pushReplacementNamed( destinationRoute, arguments: routeArguments,