Spaces:
Sleeping
Sleeping
File size: 2,504 Bytes
d381a4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | // Base64 to Uint8Array converter
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
export async function subscribeToPush(token) {
if (!('serviceWorker' in navigator) || !('PushManager' in window)) {
console.warn('Push messaging is not supported.');
return false;
}
try {
const registration = await navigator.serviceWorker.ready;
// Get VAPID public key from backend
const res = await fetch('http://localhost:3001/api/push/vapid-public-key', {
headers: { 'Authorization': `Bearer ${token}` }
});
if (!res.ok) throw new Error('Failed to fetch VAPID public key');
const { publicKey } = await res.json();
const convertedVapidKey = urlBase64ToUint8Array(publicKey);
const pushSubscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: convertedVapidKey
});
// Send subscription to backend
const subRes = await fetch('http://localhost:3001/api/push/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(pushSubscription)
});
if (!subRes.ok) throw new Error('Failed to save subscription on server');
return true;
} catch (error) {
console.error('Error subscribing to push notifications', error);
return false;
}
}
export async function unsubscribeFromPush(token) {
try {
const registration = await navigator.serviceWorker.ready;
const subscription = await registration.pushManager.getSubscription();
if (subscription) {
await subscription.unsubscribe();
// Delete from backend
await fetch('http://localhost:3001/api/push/subscribe', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ endpoint: subscription.endpoint })
});
return true;
}
return false;
} catch (error) {
console.error('Error unsubscribing from push notifications', error);
return false;
}
}
|