Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 12s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 10s
Gemini PR Review / Gemini PR Review (pull_request) Successful in 14s
Laravel Larastan / larastan (pull_request) Failing after 2m28s
Laravel Pint / pint (pull_request) Failing after 2m37s
44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
async function registerPush() {
|
|
try {
|
|
console.log("Registering for push notifications");
|
|
|
|
if (!('serviceWorker' in navigator)) {
|
|
console.error("ServiceWorker not supported");
|
|
return;
|
|
}
|
|
|
|
const permission = await Notification.requestPermission();
|
|
console.log("Permission:", permission);
|
|
|
|
if (permission !== 'granted') {
|
|
console.warn("Notification permission denied");
|
|
return;
|
|
}
|
|
|
|
const registration = await navigator.serviceWorker.register('/sw.js');
|
|
console.log("SW registered:", registration);
|
|
|
|
const subscription = await registration.pushManager.subscribe({
|
|
userVisibleOnly: true,
|
|
applicationServerKey: vapidKey
|
|
});
|
|
|
|
console.log("Subscription created:", subscription);
|
|
|
|
const res = await fetch('/push/subscribe', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': csrfToken
|
|
},
|
|
body: JSON.stringify(subscription)
|
|
});
|
|
|
|
console.log("Server response:", await res.text());
|
|
|
|
alert("Push enabled ✅");
|
|
} catch (e) {
|
|
console.error("Push registration failed ❌", e);
|
|
}
|
|
}
|