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) {