db = $db; $this->mailer = $mailer; } public function createNotification($user_id, $type, $message, $data = null) { $stmt = $this->db->prepare("INSERT INTO notifications (user_id, type, message, data, created_at) VALUES (?, ?, ?, ?, NOW())"); $data_json = $data ? json_encode($data) : null; $stmt->bind_param('isss', $user_id, $type, $message, $data_json); $stmt->execute(); // Send email notification if enabled $this->sendEmailNotification($user_id, $type, $message); // Send push notification if enabled $this->sendPushNotification($user_id, $type, $message); return $stmt->insert_id; } public function getUserNotifications($user_id, $limit = 10, $offset = 0) { $stmt = $this->db->prepare("SELECT * FROM notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?"); $stmt->bind_param('iii', $user_id, $limit, $offset); $stmt->execute(); return $stmt->get_result()->fetch_all(MYSQLI_ASSOC); } public function markAsRead($notification_id, $user_id) { $stmt = $this->db->prepare("UPDATE notifications SET read_at = NOW() WHERE id = ? AND user_id = ?"); $stmt->bind_param('ii', $notification_id, $user_id); return $stmt->execute(); } public function markAllAsRead($user_id) { $stmt = $this->db->prepare("UPDATE notifications SET read_at = NOW() WHERE user_id = ? AND read_at IS NULL"); $stmt->bind_param('i', $user_id); return $stmt->execute(); } public function getUnreadCount($user_id) { $stmt = $this->db->prepare("SELECT COUNT(*) as count FROM notifications WHERE user_id = ? AND read_at IS NULL"); $stmt->bind_param('i', $user_id); $stmt->execute(); $result = $stmt->get_result()->fetch_assoc(); return $result['count']; } private function sendEmailNotification($user_id, $type, $message) { // Get user email $stmt = $this->db->prepare("SELECT email, notification_preferences FROM users WHERE id = ?"); $stmt->bind_param('i', $user_id); $stmt->execute(); $user = $stmt->get_result()->fetch_assoc(); if ($user && $this->shouldSendEmail($user['notification_preferences'], $type)) { $this->mailer->sendEmail( $user['email'], "New Notification from ShubraVeil", $message ); } } private function sendPushNotification($user_id, $type, $message) { // Get user's push notification token $stmt = $this->db->prepare("SELECT push_token, notification_preferences FROM users WHERE id = ?"); $stmt->bind_param('i', $user_id); $stmt->execute(); $user = $stmt->get_result()->fetch_assoc(); if ($user && $this->shouldSendPush($user['notification_preferences'], $type) && $user['push_token']) { // Initialize Firebase $firebase = new \Kreait\Firebase\Factory(); $messaging = $firebase->createMessaging(); $message = \Kreait\Firebase\Messaging\CloudMessage::withTarget('token', $user['push_token']) ->withNotification([ 'title' => 'ShubraVeil', 'body' => $message ]); try { $messaging->send($message); } catch (\Exception $e) { // Log error but don't throw error_log("Push notification failed: " . $e->getMessage()); } } } private function shouldSendEmail($preferences, $type) { $prefs = json_decode($preferences, true); return isset($prefs['email'][$type]) ? $prefs['email'][$type] : true; } private function shouldSendPush($preferences, $type) { $prefs = json_decode($preferences, true); return isset($prefs['push'][$type]) ? $prefs['push'][$type] : true; } }