From 52947f7b2168afd74e10644e00705b095317fd81 Mon Sep 17 00:00:00 2001 From: Agent Date: Sat, 13 Jun 2026 17:48:53 -0400 Subject: [PATCH] fix: filter FCM sends by notifications_enabled preference --- src/Controllers/AdminController.php | 12 ++++++++++++ src/Services/FCMService.php | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Controllers/AdminController.php b/src/Controllers/AdminController.php index dc6dcaa..aac764f 100755 --- a/src/Controllers/AdminController.php +++ b/src/Controllers/AdminController.php @@ -299,6 +299,18 @@ class AdminController return; } + if ($userId) { + $userModel = new User(); + $targetUser = $userModel->findById($userId); + if (!$targetUser || empty($targetUser['notifications_enabled'])) { + $this->view->json([ + 'success' => true, + 'message' => 'Notification saved to database. User has notifications disabled.', + ]); + return; + } + } + $notificationModel = new Notification(); $noteId = $notificationModel->create([ 'user_id' => $userId, diff --git a/src/Services/FCMService.php b/src/Services/FCMService.php index dd9190e..aeaa0e5 100644 --- a/src/Services/FCMService.php +++ b/src/Services/FCMService.php @@ -54,6 +54,12 @@ class FCMService { if (!$this->isConfigured()) return false; + $enabled = $this->db->fetch( + 'SELECT notifications_enabled FROM users WHERE id = ? AND notifications_enabled = 1', + [$userId] + ); + if (!$enabled) return false; + $tokens = $this->db->fetchAll( 'SELECT id, token FROM user_fcm_tokens WHERE user_id = ?', [$userId] @@ -78,7 +84,11 @@ class FCMService { if (!$this->isConfigured()) return 0; - $tokens = $this->db->fetchAll('SELECT id, token FROM user_fcm_tokens'); + $tokens = $this->db->fetchAll( + 'SELECT f.id, f.token FROM user_fcm_tokens f + JOIN users u ON u.id = f.user_id + WHERE u.notifications_enabled = 1' + ); if (empty($tokens)) return 0; $sent = 0;