Push notification tap redirect to corressponding ticket or task

This commit is contained in:
2026-03-02 21:09:38 +08:00
parent eb49329b16
commit 5713581992
3 changed files with 75 additions and 58 deletions
+51 -38
View File
@@ -1,25 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:go_router/go_router.dart';
import '../models/notification_item.dart';
import '../providers/notifications_provider.dart';
import '../providers/notification_navigation_provider.dart';
import '../routing/app_router.dart';
/// Wraps the app and installs both a Supabase realtime listener and the
/// FCM handlers described in the frontend design.
///
/// The navigator key is required so that snackbars and navigation can be
/// triggered from outside the widget tree (e.g. from realtime callbacks).
/// Navigation is performed via the [GoRouter] instance obtained from
/// [appRouterProvider], so this widget does **not** require an external
/// navigator key.
class NotificationBridge extends ConsumerStatefulWidget {
const NotificationBridge({
required this.navigatorKey,
required this.child,
super.key,
});
const NotificationBridge({required this.child, super.key});
final GlobalKey<NavigatorState> navigatorKey;
final Widget child;
@override
@@ -55,26 +51,36 @@ class _NotificationBridgeState extends ConsumerState<NotificationBridge>
}
}
void _showBanner(String type, NotificationItem item) {
final ctx = widget.navigatorKey.currentState?.overlay?.context;
if (ctx == null) return;
ScaffoldMessenger.of(ctx).showSnackBar(
SnackBar(
content: Text('New $type received!'),
action: SnackBarAction(
label: 'View',
onPressed: () => _navigateToNotification(item),
),
),
);
/// Navigate to the task or ticket detail screen using GoRouter.
void _goToDetail({String? taskId, String? ticketId}) {
final router = ref.read(appRouterProvider);
if (taskId != null && taskId.isNotEmpty) {
router.go('/tasks/$taskId');
} else if (ticketId != null && ticketId.isNotEmpty) {
router.go('/tickets/$ticketId');
}
}
void _navigateToNotification(NotificationItem item) {
widget.navigatorKey.currentState?.pushNamed(
'/notification-detail',
arguments: item,
);
void _showBanner(String type, NotificationItem item) {
// Use a post-frame callback so that the ScaffoldMessenger from
// MaterialApp is available in the element tree.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
try {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('New $type received!'),
action: SnackBarAction(
label: 'View',
onPressed: () =>
_goToDetail(taskId: item.taskId, ticketId: item.ticketId),
),
),
);
} catch (_) {
// ScaffoldMessenger not available yet (before first frame) — ignore.
}
});
}
void _setupFcmHandlers() {
@@ -90,10 +96,20 @@ class _NotificationBridgeState extends ConsumerState<NotificationBridge>
});
}
/// Extract task/ticket IDs from the FCM data payload and navigate.
///
/// The FCM data map contains keys such as `task_id` / `taskId` /
/// `ticket_id` / `ticketId` — these are the same keys used in the
/// background handler ([_firebaseMessagingBackgroundHandler]).
void _handleMessageTap(RemoteMessage message) {
final data = message.data.cast<String, dynamic>();
final item = NotificationItem.fromMap(data);
_navigateToNotification(item);
final data = message.data;
final String? taskId = (data['task_id'] ?? data['taskId'] ?? data['task'])
?.toString();
final String? ticketId =
(data['ticket_id'] ?? data['ticketId'] ?? data['ticket'])?.toString();
_goToDetail(taskId: taskId, ticketId: ticketId);
}
@override
@@ -115,19 +131,16 @@ class _NotificationBridgeState extends ConsumerState<NotificationBridge>
_prevList = nextList;
});
// Listen for pending navigation from notification taps
// Listen for pending navigation from local-notification taps
ref.listen<PendingNavigation?>(pendingNotificationNavigationProvider, (
previous,
next,
) {
if (next != null) {
final type = next.type;
final id = next.id;
if (type == 'ticket') {
GoRouter.of(context).go('/tickets/$id');
} else if (type == 'task') {
GoRouter.of(context).go('/tasks/$id');
}
_goToDetail(
taskId: next.type == 'task' ? next.id : null,
ticketId: next.type == 'ticket' ? next.id : null,
);
// Clear the pending navigation after handling
ref.read(pendingNotificationNavigationProvider.notifier).state = null;
}