replay pending notification tap after login

This commit is contained in:
Mohamad Mahdi Jebeli 2026-06-21 15:55:51 +03:30
parent 80aa5822d3
commit b793cb2a4b
3 changed files with 38 additions and 0 deletions

View File

@ -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.dart';
import 'package:didvan/services/network/request_helper.dart'; import 'package:didvan/services/network/request_helper.dart';
import 'package:didvan/services/notification/firebase_api.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/services/storage/storage.dart';
import 'package:didvan/utils/action_sheet.dart'; import 'package:didvan/utils/action_sheet.dart';
@ -188,6 +189,13 @@ class UserProvider extends CoreProvier {
// ignore: discarded_futures // ignore: discarded_futures
fetchWelcomeMessage(); 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; return true;
} catch (e) { } catch (e) {
print("UserProvider: Exception processing user info: $e"); print("UserProvider: Exception processing user info: $e");

View File

@ -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<void> 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 /// Read a pending notification payload (if any) from cross-isolate
/// storage and hand it off to [HomeWidgetRepository.data] without /// storage and hand it off to [HomeWidgetRepository.data] without
/// navigating. Use this during splash where you want the existing splash /// navigating. Use this during splash where you want the existing splash

View File

@ -378,6 +378,18 @@ class _SplashState extends State<Splash> with SingleTickerProviderStateMixin {
return; 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( Navigator.of(context).pushReplacementNamed(
destinationRoute, destinationRoute,
arguments: routeArguments, arguments: routeArguments,