refactor: enhance push notification routing with full payload handling and add socket connection state management
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
@@ -27,7 +28,38 @@ class PushNotificationService {
|
||||
bool _initialized = false;
|
||||
|
||||
/// Callback when user taps a notification — set by the app to navigate.
|
||||
void Function(String? actionUrl)? onNotificationTap;
|
||||
/// Receives the full FCM data payload so the app can route by `type`
|
||||
/// (e.g. 'message' → open chat) instead of only relying on `actionUrl`.
|
||||
///
|
||||
/// When this setter is assigned and a pending tap was buffered (because the
|
||||
/// terminated-state tap fired before the app UI was ready), the buffered
|
||||
/// tap is replayed immediately.
|
||||
void Function(Map<String, dynamic> data)? _onNotificationTap;
|
||||
Map<String, dynamic>? _pendingTap;
|
||||
|
||||
set onNotificationTap(void Function(Map<String, dynamic> data)? handler) {
|
||||
_onNotificationTap = handler;
|
||||
// Replay any buffered tap once a listener is attached
|
||||
if (handler != null && _pendingTap != null) {
|
||||
final buffered = _pendingTap!;
|
||||
_pendingTap = null;
|
||||
// Defer to next microtask so the caller's initState finishes first
|
||||
Future.microtask(() => handler(buffered));
|
||||
}
|
||||
}
|
||||
|
||||
void Function(Map<String, dynamic> data)? get onNotificationTap =>
|
||||
_onNotificationTap;
|
||||
|
||||
void _dispatchTap(Map<String, dynamic> data) {
|
||||
final handler = _onNotificationTap;
|
||||
if (handler != null) {
|
||||
handler(data);
|
||||
} else {
|
||||
// No listener yet — buffer for replay once one attaches
|
||||
_pendingTap = data;
|
||||
}
|
||||
}
|
||||
|
||||
/// The conversation ID currently being viewed — suppress message push
|
||||
/// notifications for this conversation to avoid self-notifications.
|
||||
@@ -116,7 +148,17 @@ class PushNotificationService {
|
||||
),
|
||||
onDidReceiveNotificationResponse: (response) {
|
||||
final payload = response.payload;
|
||||
onNotificationTap?.call(payload);
|
||||
if (payload == null || payload.isEmpty) {
|
||||
_dispatchTap(const {});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final data = jsonDecode(payload) as Map<String, dynamic>;
|
||||
_dispatchTap(data);
|
||||
} catch (_) {
|
||||
// Legacy payloads may be a bare actionUrl string
|
||||
_dispatchTap({'actionUrl': payload});
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -200,15 +242,16 @@ class PushNotificationService {
|
||||
icon: '@mipmap/ic_launcher',
|
||||
),
|
||||
),
|
||||
payload: message.data['actionUrl'],
|
||||
// Encode the full FCM data as JSON so the tap handler can route
|
||||
// by `type` (e.g. open the specific chat for message notifications).
|
||||
payload: jsonEncode(message.data),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _onMessageOpenedApp(RemoteMessage message) {
|
||||
debugPrint('[FCM] Notification tap: ${message.data}');
|
||||
final actionUrl = message.data['actionUrl'] as String?;
|
||||
onNotificationTap?.call(actionUrl);
|
||||
_dispatchTap(Map<String, dynamic>.from(message.data));
|
||||
}
|
||||
|
||||
/// Unregister FCM token from backend (call on logout).
|
||||
|
||||
Reference in New Issue
Block a user