feat: trigger local notification directly from incoming WebSocket new_message event
This commit is contained in:
@@ -153,6 +153,36 @@ class FirebaseService extends GetxService {
|
||||
);
|
||||
}
|
||||
|
||||
void showLocalNotificationFromData(Map<String, dynamic> data) {
|
||||
final chatId = data['chatId'];
|
||||
final name = data['name'] ?? 'WhatsApp';
|
||||
final body = data['body'] ?? 'New Message';
|
||||
|
||||
// Smart Notification: Only show if we are NOT currently in this chat
|
||||
final activeChatId = Get.find<WhatsAppService>().activeChatId.value;
|
||||
if (chatId != null && activeChatId == chatId) {
|
||||
return; // Silent
|
||||
}
|
||||
|
||||
const androidDetails = AndroidNotificationDetails(
|
||||
'whatsapp_channel',
|
||||
'WhatsApp Messages',
|
||||
importance: Importance.max,
|
||||
priority: Priority.high,
|
||||
ticker: 'ticker',
|
||||
);
|
||||
const iosDetails = DarwinNotificationDetails();
|
||||
const details = NotificationDetails(android: androidDetails, iOS: iosDetails);
|
||||
|
||||
_localNotifications.show(
|
||||
DateTime.now().microsecond,
|
||||
name,
|
||||
body,
|
||||
details,
|
||||
payload: jsonEncode({'chatId': chatId, 'name': name}),
|
||||
);
|
||||
}
|
||||
|
||||
void _onNotificationTap(NotificationResponse response) {
|
||||
if (response.payload != null) {
|
||||
final data = jsonDecode(response.payload!);
|
||||
@@ -165,7 +195,6 @@ class FirebaseService extends GetxService {
|
||||
final name = data['name'] ?? 'Chat';
|
||||
|
||||
if (chatId != null) {
|
||||
// Mock a conversation model to navigate to ChatScreen
|
||||
final dummyChat = ConversationModel(
|
||||
id: chatId,
|
||||
name: name,
|
||||
@@ -176,7 +205,7 @@ class FirebaseService extends GetxService {
|
||||
isMuted: false,
|
||||
);
|
||||
|
||||
Get.to(() => ChatScreen(conversation: dummyChat));
|
||||
Get.to(() => ChatScreen(conversation: dummyChat), preventDuplicates: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user