fix push changes 2
This commit is contained in:
parent
6ab94bb3cd
commit
a7b349241c
@ -4,14 +4,24 @@ function checkServiceWorkerSupport() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function isPushRegistered() {
|
async function isPushRegistered() {
|
||||||
checkServiceWorkerSupport();
|
checkServiceWorkerSupport();
|
||||||
try {
|
try {
|
||||||
const reg = await navigator.serviceWorker.ready;
|
const regs = await navigator.serviceWorker.getRegistrations();
|
||||||
const sub = await reg.pushManager.getSubscription();
|
for (const reg of regs) {
|
||||||
return !!sub;
|
if (!reg.pushManager) continue;
|
||||||
} catch {
|
|
||||||
return false;
|
const sub = await reg.pushManager.getSubscription();
|
||||||
}
|
if (sub) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reg = await navigator.serviceWorker.getRegistration();
|
||||||
|
if (!reg || !reg.pushManager) return false;
|
||||||
|
|
||||||
|
const sub = await reg.pushManager.getSubscription();
|
||||||
|
return !!sub;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function unregisterPush() {
|
async function unregisterPush() {
|
||||||
@ -42,17 +52,14 @@ async function registerPush(publicKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const registration = await navigator.serviceWorker.register("/assets/custom/serviceWorker.js");
|
const registration = await navigator.serviceWorker.register("/assets/custom/serviceWorker.js");
|
||||||
console.log("Service Worker registered");
|
|
||||||
|
|
||||||
let subscription = await registration.pushManager.getSubscription();
|
let subscription = await registration.pushManager.getSubscription();
|
||||||
|
|
||||||
if (!subscription) {
|
if (!subscription) {
|
||||||
const applicationServerKey = urlBase64ToUint8Array(publicKey);
|
const applicationServerKey = urlBase64ToUint8Array(publicKey);
|
||||||
subscription = await registration.pushManager.subscribe({
|
subscription = await registration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
applicationServerKey
|
applicationServerKey
|
||||||
});
|
});
|
||||||
console.log("Push subscription obtained");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(`${window.GLOBAL_ENV.API_ENDPOINT}/v9/users/@me/devices`, {
|
const response = await fetch(`${window.GLOBAL_ENV.API_ENDPOINT}/v9/users/@me/devices`, {
|
||||||
@ -67,9 +74,7 @@ async function registerPush(publicKey) {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (!response.ok) {
|
||||||
console.log("Device registered successfully");
|
|
||||||
} else {
|
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
throw new Error("Failed to register: ", text)
|
throw new Error("Failed to register: ", text)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user