owncast/web/services/notifications-service.ts
Gabe Kangas e50b23d081
Cleanup unused Javascript (#3027)
* chore(js): be stricter about dead code warnings

* chore(js): remove dead code and unused exports

* rebase

* chore: remove unused files

* chore(deps): remove unused prop-types dep

* chore(js): remove unused function

* chore(deps): remove + check unused deps

* chore(js): remove unused exports. Closes #3036
2023-05-20 21:15:25 -07:00

42 lines
1.3 KiB
TypeScript

export async function saveNotificationRegistration(channel, destination, accessToken) {
console.log('saveNotificationRegistration');
const URL_REGISTER_NOTIFICATION = `/api/notifications/register`;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ channel, destination }),
};
await fetch(`${URL_REGISTER_NOTIFICATION}?accessToken=${accessToken}`, options);
}
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);
// eslint-disable-next-line no-plusplus
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);
}