refactor: enhance push notification routing with full payload handling and add socket connection state management

This commit is contained in:
pradeepkumar
2026-04-08 21:24:35 +05:30
parent ca81a73e49
commit 509eae2e88
3 changed files with 118 additions and 9 deletions

View File

@@ -20,18 +20,69 @@ class _AppShellState extends State<AppShell> {
void initState() {
super.initState();
// Wire push notification taps to GoRouter navigation
PushNotificationService().onNotificationTap = (actionUrl) {
PushNotificationService().onNotificationTap = (data) {
if (!mounted) return;
if (actionUrl == null || actionUrl.isEmpty) {
debugPrint('[AppShell] Notification tap: $data');
// Extract the conversationId from every possible source — FCM payloads
// from different backends/versions may use different keys, and the
// link/actionUrl may carry it as a query parameter instead.
final convId = _extractConversationId(data);
final type = (data['type'] as String?)?.toLowerCase();
final link = (data['link'] as String?) ?? (data['actionUrl'] as String?);
final looksLikeMessage = type == 'message' ||
(link != null && link.contains('/message'));
if (looksLikeMessage && convId != null && convId.isNotEmpty) {
debugPrint('[AppShell] → /messages/chat/$convId');
context.go('/messages/chat/$convId');
return;
}
if (type == 'connection_request' || type == 'connection_response') {
context.go('/notifications');
return;
}
// Translate web routes to mobile routes
final mobileRoute = _translateRoute(actionUrl);
// Fall back to actionUrl translation (web-style URLs)
if (link == null || link.isEmpty) {
context.go('/notifications');
return;
}
final mobileRoute = _translateRoute(link);
debugPrint('[AppShell] → $mobileRoute (from link=$link)');
context.go(mobileRoute);
};
}
/// Try every known source for a conversation id — direct keys, or
/// query parameters on the link/actionUrl.
String? _extractConversationId(Map<String, dynamic> data) {
// Direct keys (backend sends `conversationId`)
for (final key in const [
'conversationId',
'conversation_id',
'convId',
'chatId',
]) {
final v = data[key];
if (v is String && v.isNotEmpty) return v;
}
// Query parameter on the link or actionUrl
for (final key in const ['link', 'actionUrl']) {
final raw = data[key];
if (raw is String && raw.isNotEmpty) {
try {
final uri = Uri.parse(raw);
final qp = uri.queryParameters['conversationId'] ??
uri.queryParameters['conversation_id'];
if (qp != null && qp.isNotEmpty) return qp;
} catch (_) {}
}
}
return null;
}
/// Translate web-style actionUrl to mobile route.
String _translateRoute(String url) {
final uri = Uri.parse(url);