feat: trigger local notification directly from incoming WebSocket new_message event

This commit is contained in:
Hamza-Ayed
2026-05-18 18:19:03 +03:00
parent cfc1fd0a8e
commit 92d59b0f30
2 changed files with 53 additions and 2 deletions

View File

@@ -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<FirebaseService>().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) {