feat: ignore background and local notifications for muted chats
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:firebase_messaging/firebase_messaging.dart';
|
|||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
import '../controllers/conversations_controller.dart';
|
||||||
import '../models/conversation_model.dart';
|
import '../models/conversation_model.dart';
|
||||||
import '../screens/chat_screen.dart';
|
import '../screens/chat_screen.dart';
|
||||||
import 'whatsapp_service.dart';
|
import 'whatsapp_service.dart';
|
||||||
@@ -173,6 +174,16 @@ class FirebaseService extends GetxService {
|
|||||||
return; // Silent
|
return; // Silent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do not show local notification if the chat is muted in user's conversations list
|
||||||
|
if (chatId != null && Get.isRegistered<ConversationsController>()) {
|
||||||
|
final controller = Get.find<ConversationsController>();
|
||||||
|
final index = controller.conversations.indexWhere((c) => c.id == chatId);
|
||||||
|
if (index != -1 && controller.conversations[index].isMuted) {
|
||||||
|
print('[LOCAL NOTIF] Skipping: Chat $chatId is muted by user.');
|
||||||
|
return; // Silent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const androidDetails = AndroidNotificationDetails(
|
const androidDetails = AndroidNotificationDetails(
|
||||||
'whatsapp_channel',
|
'whatsapp_channel',
|
||||||
'WhatsApp Messages',
|
'WhatsApp Messages',
|
||||||
|
|||||||
@@ -334,6 +334,12 @@ function initWhatsApp() {
|
|||||||
// Trigger background push notification if not sent by me
|
// Trigger background push notification if not sent by me
|
||||||
if (!msg.fromMe) {
|
if (!msg.fromMe) {
|
||||||
try {
|
try {
|
||||||
|
const chat = await msg.getChat();
|
||||||
|
if (chat.isMuted) {
|
||||||
|
console.log(`[FCM] Push skipped for muted chat: ${msg.from}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const contact = await msg.getContact();
|
const contact = await msg.getContact();
|
||||||
const senderName = contact.name || contact.pushname || msg.from.split('@')[0];
|
const senderName = contact.name || contact.pushname || msg.from.split('@')[0];
|
||||||
let body = msg.body || '';
|
let body = msg.body || '';
|
||||||
|
|||||||
Reference in New Issue
Block a user