2022-05-30 07:51:00 +03:00
|
|
|
export async function saveNotificationRegistration(channel, destination, accessToken) {
|
2023-04-16 06:10:34 +03:00
|
|
|
console.log('saveNotificationRegistration');
|
2022-05-30 07:51:00 +03:00
|
|
|
const URL_REGISTER_NOTIFICATION = `/api/notifications/register`;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ channel, destination }),
|
|
|
|
};
|
|
|
|
|
2023-04-16 06:10:34 +03:00
|
|
|
await fetch(`${URL_REGISTER_NOTIFICATION}?accessToken=${accessToken}`, options);
|
2022-05-30 07:51:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isPushNotificationSupported() {
|
|
|
|
return 'serviceWorker' in navigator && 'PushManager' in window;
|
|
|
|
}
|
|
|
|
|
|
|
|
function urlBase64ToUint8Array(base64String: string) {
|
|
|
|
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);
|
|
|
|
|
2023-04-16 06:10:34 +03:00
|
|
|
// eslint-disable-next-line no-plusplus
|
2022-05-30 07:51:00 +03:00
|
|
|
for (let i = 0; i < rawData.length; ++i) {
|
|
|
|
outputArray[i] = rawData.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return outputArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function registerWebPushNotifications(vapidPublicKey) {
|
|
|
|
const registration = await navigator.serviceWorker.ready;
|
|
|
|
let subscription = await registration.pushManager.getSubscription();
|
|
|
|
|
|
|
|
if (!subscription) {
|
|
|
|
subscription = await registration.pushManager.subscribe({
|
|
|
|
userVisibleOnly: true,
|
|
|
|
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(subscription);
|
|
|
|
}
|