0.12.47 fix push messages

This commit is contained in:
SDE
2024-05-05 13:37:39 +03:00
parent 474bb9f02e
commit f8caafb2a9
3 changed files with 26 additions and 6 deletions

View File

@@ -30,6 +30,26 @@ const showNotAllowed = (message) => {
button.setAttribute('disabled', 'true');
};
// urlB64ToUint8Array is a magic function that will encode the base64 public key
// to Array buffer which is needed by the subscription option
function urlB64ToUint8Array2(base64String) {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4)
const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/')
const rawData = atob(base64)
const outputArray = new Uint8Array(rawData.length)
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i)
}
return outputArray
}
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
@@ -55,7 +75,7 @@ const subscribe = async (reg) => {
const options = {
userVisibleOnly: true,
// if key exists, create applicationServerKey property
...(key && {applicationServerKey: urlB64ToUint8Array(key)})
...(key && {applicationServerKey: urlB64ToUint8Array2(key)})
};
const sub = await reg.pushManager.subscribe(options);