changed registerPush method logic in push.js
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 13s
Gemini PR Review / Gemini PR Review (pull_request) Successful in 18s
Laravel Pint / pint (pull_request) Failing after 8m22s
Laravel Larastan / larastan (pull_request) Failing after 12m1s
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 13s
Gemini PR Review / Gemini PR Review (pull_request) Successful in 18s
Laravel Pint / pint (pull_request) Failing after 8m22s
Laravel Larastan / larastan (pull_request) Failing after 12m1s
This commit is contained in:
@@ -1,3 +1,53 @@
|
|||||||
|
// 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
|
||||||
|
// // });
|
||||||
|
// const subscription = await registration.pushManager.subscribe({
|
||||||
|
// userVisibleOnly: true,
|
||||||
|
// applicationServerKey: urlBase64ToUint8Array(vapidKey),
|
||||||
|
// });
|
||||||
|
|
||||||
|
// console.log('VAPID key:', 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);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
async function registerPush() {
|
async function registerPush() {
|
||||||
try {
|
try {
|
||||||
console.log("Registering for push notifications");
|
console.log("Registering for push notifications");
|
||||||
@@ -8,30 +58,29 @@ async function registerPush() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const permission = await Notification.requestPermission();
|
const permission = await Notification.requestPermission();
|
||||||
console.log("Permission:", permission);
|
|
||||||
|
|
||||||
if (permission !== 'granted') {
|
if (permission !== 'granted') {
|
||||||
console.warn("Notification permission denied");
|
console.warn("Notification permission denied");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const registration = await navigator.serviceWorker.register('/sw.js');
|
const registration = await navigator.serviceWorker.register('/sw.js');
|
||||||
console.log("SW registered:", registration);
|
|
||||||
|
|
||||||
// const subscription = await registration.pushManager.subscribe({
|
// ✅ GET first
|
||||||
// userVisibleOnly: true,
|
let subscription = await registration.pushManager.getSubscription();
|
||||||
// applicationServerKey: vapidKey
|
|
||||||
// });
|
// ✅ CREATE only if not exists
|
||||||
const subscription = await registration.pushManager.subscribe({
|
if (!subscription) {
|
||||||
|
subscription = await registration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
applicationServerKey: urlBase64ToUint8Array(vapidKey),
|
applicationServerKey: urlBase64ToUint8Array(vapidKey),
|
||||||
});
|
});
|
||||||
|
console.log("New subscription created");
|
||||||
|
} else {
|
||||||
|
console.log("Existing subscription reused");
|
||||||
|
}
|
||||||
|
|
||||||
console.log('VAPID key:', vapidKey);
|
// 🔥 ALWAYS send to backend
|
||||||
|
await fetch('/push/subscribe', {
|
||||||
console.log("Subscription created:", subscription);
|
|
||||||
|
|
||||||
const res = await fetch('/push/subscribe', {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -40,14 +89,13 @@ async function registerPush() {
|
|||||||
body: JSON.stringify(subscription)
|
body: JSON.stringify(subscription)
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Server response:", await res.text());
|
|
||||||
|
|
||||||
alert("Push enabled ✅");
|
alert("Push enabled ✅");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Push registration failed ❌", e);
|
console.error("Push registration failed ❌", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function urlBase64ToUint8Array(base64String) {
|
function urlBase64ToUint8Array(base64String) {
|
||||||
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
||||||
const base64 = (base64String + padding)
|
const base64 = (base64String + padding)
|
||||||
|
|||||||
Reference in New Issue
Block a user