SHA256
14-04-2026
То что дела ай и то во что надо влить изменнеия
This commit is contained in:
@@ -11,34 +11,100 @@ function urlBase64ToUint8Array(base64String) {
|
||||
return outputArray;
|
||||
}
|
||||
|
||||
export async function initPwaPush({ authService }) {
|
||||
if (!('serviceWorker' in navigator) || !('PushManager' in window)) return;
|
||||
export async function initPwaPush({ authService, onLog = null }) {
|
||||
const log = (entry) => {
|
||||
if (typeof onLog === 'function') onLog(entry);
|
||||
};
|
||||
|
||||
if (!('serviceWorker' in navigator) || !('PushManager' in window)) {
|
||||
log({
|
||||
level: 'warn',
|
||||
source: 'web-push',
|
||||
message: 'Web Push недоступен: нет serviceWorker или PushManager',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const vapidPublicKey = window.__SHINE_WEBPUSH_VAPID_PUBLIC_KEY__ || '';
|
||||
if (!vapidPublicKey) return;
|
||||
if (!vapidPublicKey) {
|
||||
log({
|
||||
level: 'warn',
|
||||
source: 'web-push',
|
||||
message: 'Web Push отключен: не задан публичный VAPID ключ',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const registration = await navigator.serviceWorker.register('./firebase-messaging-sw.js');
|
||||
log({
|
||||
level: 'info',
|
||||
source: 'web-push',
|
||||
message: 'Service Worker зарегистрирован',
|
||||
details: { scope: registration.scope },
|
||||
});
|
||||
|
||||
const permission = await Notification.requestPermission();
|
||||
if (permission !== 'granted') return;
|
||||
if (permission !== 'granted') {
|
||||
log({
|
||||
level: 'warn',
|
||||
source: 'web-push',
|
||||
message: `Разрешение на уведомления: ${permission}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
log({
|
||||
level: 'info',
|
||||
source: 'web-push',
|
||||
message: 'Разрешение на уведомления получено',
|
||||
});
|
||||
|
||||
let sub = await registration.pushManager.getSubscription();
|
||||
let isNewSubscription = false;
|
||||
if (!sub) {
|
||||
sub = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey),
|
||||
});
|
||||
isNewSubscription = true;
|
||||
}
|
||||
log({
|
||||
level: 'info',
|
||||
source: 'web-push',
|
||||
message: isNewSubscription ? 'Создана новая push-подписка' : 'Найдена существующая push-подписка',
|
||||
});
|
||||
|
||||
const serialized = JSON.stringify(sub);
|
||||
if (localStorage.getItem(LS_KEY) === serialized) return;
|
||||
const prevSerialized = localStorage.getItem(LS_KEY);
|
||||
if (prevSerialized === serialized) {
|
||||
log({
|
||||
level: 'info',
|
||||
source: 'web-push',
|
||||
message: 'Push-подписка не изменилась, отправка на сервер не требуется',
|
||||
});
|
||||
return;
|
||||
}
|
||||
localStorage.setItem(LS_KEY, serialized);
|
||||
|
||||
const json = sub.toJSON();
|
||||
const endpoint = json.endpoint || '';
|
||||
const p256dhKey = json.keys?.p256dh || '';
|
||||
const authKey = json.keys?.auth || '';
|
||||
if (!endpoint || !p256dhKey || !authKey) return;
|
||||
if (!endpoint || !p256dhKey || !authKey) {
|
||||
log({
|
||||
level: 'warn',
|
||||
source: 'web-push',
|
||||
message: 'Подписка неполная: endpoint/p256dh/auth отсутствуют',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
log({
|
||||
level: 'info',
|
||||
source: 'web-push',
|
||||
message: 'Push-токен получен, отправка на сервер',
|
||||
details: { endpoint },
|
||||
});
|
||||
|
||||
await authService.upsertPushToken({
|
||||
endpoint,
|
||||
@@ -47,7 +113,17 @@ export async function initPwaPush({ authService }) {
|
||||
platform: 'web',
|
||||
userAgent: navigator.userAgent || '',
|
||||
});
|
||||
} catch {
|
||||
// silent for MVP
|
||||
log({
|
||||
level: 'info',
|
||||
source: 'web-push',
|
||||
message: 'Push-подписка успешно отправлена на сервер',
|
||||
});
|
||||
} catch (error) {
|
||||
log({
|
||||
level: 'error',
|
||||
source: 'web-push',
|
||||
message: 'Ошибка инициализации Web Push',
|
||||
details: error?.message || 'unknown',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user