diff --git a/whatsapp_app/lib/services/firebase_service.dart b/whatsapp_app/lib/services/firebase_service.dart index 98708da..95cfedc 100644 --- a/whatsapp_app/lib/services/firebase_service.dart +++ b/whatsapp_app/lib/services/firebase_service.dart @@ -153,6 +153,36 @@ class FirebaseService extends GetxService { ); } + void showLocalNotificationFromData(Map 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().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); } } } diff --git a/whatsapp_app/lib/services/whatsapp_service.dart b/whatsapp_app/lib/services/whatsapp_service.dart index b8056f6..22a81e0 100644 --- a/whatsapp_app/lib/services/whatsapp_service.dart +++ b/whatsapp_app/lib/services/whatsapp_service.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:get/get.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; import '../config/app_config.dart'; +import 'firebase_service.dart'; enum WsStatus { disconnected, connecting, connected, waReady } @@ -93,7 +94,28 @@ class WhatsAppService extends GetxService { // Push events switch (type) { + case 'new_message': + // Trigger a local notification if the app is open (WebSocket connected) + final chatId = data['chatId']; + final msgData = data['data']; + if (msgData != null && msgData['fromMe'] != true) { + String body = msgData['body'] ?? ''; + if (body.isEmpty && msgData['hasMedia'] == true) { + body = '📷 Media/Audio message'; + } + try { + Get.find().showLocalNotificationFromData({ + 'chatId': chatId, + 'name': chatId?.split('@')[0] ?? 'WhatsApp', + 'body': body, + }); + } catch (e) { + print('[LOCAL NOTIF ERROR] $e'); + } + } + break; case 'qr': + qrData.value = data['qr']; isWaReady.value = false; if (status.value == WsStatus.waReady) {