diff --git a/backend/static/js/notifications/push-notifications.js b/backend/static/js/notifications/push-notifications.js index d5989d62..0b853f3e 100644 --- a/backend/static/js/notifications/push-notifications.js +++ b/backend/static/js/notifications/push-notifications.js @@ -74,29 +74,20 @@ class PushNotificationsManager { * @private */ async _fetchVapidKey() { - console.log('[PushNotifications][DEBUG] 🔍 Запрос VAPID ключа с сервера...'); - const response = await fetch('/api/v1/notifications/push/vapid-key/', { credentials: 'include' }); - console.log('[PushNotifications][DEBUG] 📡 Ответ сервера:', response.status, response.statusText); - if (!response.ok) { throw new Error(`Не удалось получить VAPID ключ: ${response.status}`); } const data = await response.json(); - console.log('[PushNotifications][DEBUG] 📦 Данные от сервера:', data); - this.vapidPublicKey = data.vapid_public_key; if (!this.vapidPublicKey) { throw new Error('VAPID ключ отсутствует в ответе сервера'); } - - console.log('[PushNotifications][DEBUG] ✅ VAPID ключ получен, длина:', this.vapidPublicKey.length); - console.log('[PushNotifications][DEBUG] 🔑 VAPID ключ:', this.vapidPublicKey.substring(0, 20) + '...'); } /** @@ -175,33 +166,18 @@ class PushNotificationsManager { * @private */ _urlBase64ToUint8Array(base64String) { - console.log('[PushNotifications][DEBUG] 🔧 _urlBase64ToUint8Array вход:', { - length: base64String.length, - first20: base64String.substring(0, 20), - last20: base64String.substring(base64String.length - 20) - }); - const padding = '='.repeat((4 - base64String.length % 4) % 4); - console.log('[PushNotifications][DEBUG] 🔧 Добавлено паддинга:', padding.length); - const base64 = (base64String + padding) .replace(/-/g, '+') .replace(/_/g, '/'); - console.log('[PushNotifications][DEBUG] 🔧 После замены символов, длина:', base64.length); - const rawData = window.atob(base64); - console.log('[PushNotifications][DEBUG] 🔧 После atob, длина:', rawData.length); - const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } - console.log('[PushNotifications][DEBUG] 🔧 Uint8Array готов, длина:', outputArray.length); - console.log('[PushNotifications][DEBUG] 🔧 Первые 10 байт:', Array.from(outputArray.slice(0, 10))); - return outputArray; } @@ -224,94 +200,46 @@ class PushNotificationsManager { * @returns {Promise} Успешность подписки */ async subscribe() { - console.log('[PushNotifications][DEBUG] 🚀 Начало процесса подписки'); - if (!this.isSupported || !this.swRegistration || !this.vapidPublicKey) { - console.error('[PushNotifications][DEBUG] ❌ Менеджер не инициализирован:', { - isSupported: this.isSupported, - swRegistration: !!this.swRegistration, - vapidPublicKey: !!this.vapidPublicKey - }); + console.error('[PushNotifications] Менеджер не инициализирован'); return false; } // Проверяем разрешение - console.log('[PushNotifications][DEBUG] 🔐 Проверка разрешений, текущее:', Notification.permission); - if (Notification.permission === 'denied') { - console.warn('[PushNotifications][DEBUG] ❌ Уведомления заблокированы пользователем'); + console.warn('[PushNotifications] Уведомления заблокированы'); return false; } if (Notification.permission === 'default') { - console.log('[PushNotifications][DEBUG] ❓ Запрос разрешения у пользователя...'); const permission = await this.requestPermission(); if (permission !== 'granted') { - console.log('[PushNotifications][DEBUG] ❌ Разрешение не получено:', permission); return false; } } try { - // Удаляем старую подписку, если есть (на случай смены VAPID ключей) + // Удаляем старую подписку, если есть const existingSubscription = await this.swRegistration.pushManager.getSubscription(); - console.log('[PushNotifications][DEBUG] 🔍 Существующая подписка:', existingSubscription ? 'найдена' : 'отсутствует'); - if (existingSubscription) { - console.log('[PushNotifications][DEBUG] 🗑️ Удаляем старую подписку:', existingSubscription.endpoint.substring(0, 50) + '...'); await existingSubscription.unsubscribe(); - console.log('[PushNotifications][DEBUG] ✅ Старая подписка удалена'); } - // Конвертируем VAPID ключ - console.log('[PushNotifications][DEBUG] 🔄 Конвертация VAPID ключа в Uint8Array...'); - const applicationServerKey = this._urlBase64ToUint8Array(this.vapidPublicKey); - console.log('[PushNotifications][DEBUG] ✅ Uint8Array создан, длина:', applicationServerKey.length); - console.log('[PushNotifications][DEBUG] 🔢 Первые байты:', Array.from(applicationServerKey.slice(0, 5))); - - // Создаем push-подписку - console.log('[PushNotifications][DEBUG] 📝 Создание новой подписки...'); - console.log('[PushNotifications][DEBUG] 🌐 Push Manager:', this.swRegistration.pushManager); - - // Проверяем поддерживаемые функции - const pushManager = this.swRegistration.pushManager; - console.log('[PushNotifications][DEBUG] 🔍 Push Manager возможности:', { - hasGetSubscription: typeof pushManager.getSubscription === 'function', - hasSubscribe: typeof pushManager.subscribe === 'function', - hasPermissionState: typeof pushManager.permissionState === 'function' - }); - + // Создаем новую подписку const subscription = await this.swRegistration.pushManager.subscribe({ userVisibleOnly: true, - applicationServerKey: applicationServerKey + applicationServerKey: this._urlBase64ToUint8Array(this.vapidPublicKey) }); - console.log('[PushNotifications][DEBUG] ✅ Push-подписка создана успешно!'); - console.log('[PushNotifications][DEBUG] 🌐 Endpoint:', subscription.endpoint.substring(0, 70) + '...'); - console.log('[PushNotifications][DEBUG] 🔑 Keys:', Object.keys(subscription.toJSON().keys)); - // Отправляем подписку на сервер - console.log('[PushNotifications][DEBUG] 📤 Отправка подписки на сервер...'); await this._sendSubscriptionToServer(subscription); this.isSubscribed = true; - console.log('[PushNotifications][DEBUG] 🎉 Подписка завершена успешно!'); + console.log('[PushNotifications] ✅ Подписка успешна'); return true; } catch (error) { - console.error('[PushNotifications][DEBUG] ❌ ОШИБКА ПОДПИСКИ:', error); - console.error('[PushNotifications][DEBUG] 📋 Тип ошибки:', error.name); - console.error('[PushNotifications][DEBUG] 💬 Сообщение:', error.message); - console.error('[PushNotifications][DEBUG] 📚 Stack:', error.stack); - - // Дополнительная диагностика - if (error.name === 'AbortError') { - console.error('[PushNotifications][DEBUG] 🔍 AbortError обычно означает:'); - console.error('[PushNotifications][DEBUG] 1. Невалидный VAPID ключ'); - console.error('[PushNotifications][DEBUG] 2. Проблема с push-сервисом (FCM/Mozilla)'); - console.error('[PushNotifications][DEBUG] 3. Неправильный формат applicationServerKey'); - } - + console.error('[PushNotifications] Ошибка подписки:', error); return false; } }