import env from "../../env"; import urlB64ToUint8Array from "./base64-uint8"; export const removeNotificationSubscription = (): Promise => { if ('serviceWorker' in navigator) { return navigator.serviceWorker.ready.then(function (registration) { return registration.pushManager.getSubscription() .then(function (subscription) { if (subscription) { // Unsubscribe the user return subscription.unsubscribe(); } else { return true } }) .then(function (success) { if (success) { return true // Optionally, send a request to your server to remove the subscription from your database } return false }) .catch(function (error) { console.error('Error during unsubscribe:', error); return false }); }).catch(function (error) { console.error('Service worker not ready', error) return false }) } else { console.error('Service workers are not supported.') return Promise.resolve(false) } } export const getNotificationSubscription = () => { if ('serviceWorker' in navigator) { return navigator.serviceWorker.ready.then(function (registration) { return registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlB64ToUint8Array(env.VAPID_PUB) }).then(function (subscription) { return subscription }).catch(function (error) { return null }) }).catch(function (error) { console.error('Service worker not ready', error) return null }) } else { console.error('Service workers are not supported.') } return Promise.resolve(null) } export default getNotificationSubscription