diff --git a/components/PwaManager.tsx b/components/PwaManager.tsx index e7fe746..623fd17 100644 --- a/components/PwaManager.tsx +++ b/components/PwaManager.tsx @@ -70,18 +70,6 @@ function PwaManager() { } }; - // Utility function for converting base64 string to Uint8Array - const base64ToUint8Array = base64 => { - const padding = '='.repeat((4 - (base64.length % 4)) % 4); - const b64 = (base64 + padding).replace(/-/g, '+').replace(/_/g, '/'); - const rawData = window.atob(b64); - const outputArray = new Uint8Array(rawData.length); - for (let i = 0; i < rawData.length; ++i) { - outputArray[i] = rawData.charCodeAt(i); - } - return outputArray; - }; - const subscribeToNotifications = async (e) => { try { @@ -108,7 +96,7 @@ function PwaManager() { } const sub = await registration.pushManager.subscribe({ userVisibleOnly: true, - applicationServerKey: base64ToUint8Array(vapidPublicKey) + applicationServerKey: common.base64ToUint8Array(vapidPublicKey) }); // Call your API to save subscription data on server if (session.user?.id != null) { diff --git a/src/helpers/common.js b/src/helpers/common.js index 89a7811..2a84eab 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -912,6 +912,17 @@ function adjustDateForDST(date, timezone) { } exports.adjustDateForDST = adjustDateForDST; + +export function base64ToUint8Array(base64) { + const padding = '='.repeat((4 - (base64.length % 4)) % 4); + const b64 = (base64 + padding).replace(/-/g, '+').replace(/_/g, '/'); + const rawData = window.atob(b64); + const outputArray = new Uint8Array(rawData.length); + for (let i = 0; i < rawData.length; ++i) { + outputArray[i] = rawData.charCodeAt(i); + } + return outputArray; +} // exports.getInitials = function (names) { // const parts = names.split(' '); // Split the full name into parts // if (parts.length === 0) {