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