fix: resolve duplicate background notifications and format contact names in local notification titles

This commit is contained in:
Hamza-Ayed
2026-05-18 18:53:24 +03:00
parent b3ef0b89f6
commit 123902a6b1
5 changed files with 42 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import '../models/conversation_model.dart';
@@ -158,6 +159,14 @@ class FirebaseService extends GetxService {
final name = data['name'] ?? 'WhatsApp';
final body = data['body'] ?? 'New Message';
// Only show local notifications when the app is actively in the foreground (resumed).
// If the app is in the background or suspended, the native FCM notification will handle it natively.
// This fully prevents duplicate notifications!
if (WidgetsBinding.instance.lifecycleState != AppLifecycleState.resumed) {
print('[FCM] Skipping WebSocket local notification: app is in background.');
return;
}
// Smart Notification: Only show if we are NOT currently in this chat
final activeChatId = Get.find<WhatsAppService>().activeChatId.value;
if (chatId != null && activeChatId == chatId) {

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 'contacts_service.dart';
import 'firebase_service.dart';
enum WsStatus { disconnected, connecting, connected, waReady }
@@ -104,9 +105,15 @@ class WhatsAppService extends GetxService {
body = '📷 Media/Audio message';
}
try {
final String cleanNumber = chatId?.split('@')[0] ?? '';
final String senderName = Get.find<ContactsService>().getContactName(
cleanNumber,
cleanNumber.isNotEmpty ? '+$cleanNumber' : 'WhatsApp',
);
Get.find<FirebaseService>().showLocalNotificationFromData({
'chatId': chatId,
'name': chatId?.split('@')[0] ?? 'WhatsApp',
'name': senderName,
'body': body,
});
} catch (e) {