Skip to content

Commit 99ce70e

Browse files
committed
fix: notification toggle sync with permission, toasts, and smarter permission request
1 parent f8c4154 commit 99ce70e

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

js/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ window.addEventListener("notificationPermissionDenied", () => {
317317
);
318318
});
319319

320+
window.addEventListener("notificationPermissionGranted", () => {
321+
showToast("✓ Notifications enabled. You'll get reminders for overdue tasks.", "success", 3500);
322+
});
323+
320324
/* ====================== Main Functions ===================== */
321325
// Switch Tabs
322326
function switchTabs(e, btn) {

js/pushNotificationsHandler.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,58 @@ async function requestNotificationPermission() {
1616
return false;
1717
}
1818

19+
// Sync checkbox and localStorage with actual browser permission (e.g. after user revokes in settings)
20+
function syncNotificationToggleWithPermission() {
21+
if (!("Notification" in window)) return;
22+
const toggle = document.getElementById("switchCheckChecked");
23+
if (!toggle) return;
24+
if (Notification.permission !== "granted") {
25+
toggle.checked = false;
26+
localStorage.setItem("notificationsEnabled", "false");
27+
}
28+
}
29+
1930
// Notification Toggler in UI
2031
document.addEventListener("DOMContentLoaded", () => {
2132
const toggle = document.getElementById("switchCheckChecked");
2233
if (!toggle) return;
2334

24-
// Load saved notification preference from localStorage
35+
// Load saved preference, but keep checkbox in sync with actual browser permission
2536
const savedPreference = localStorage.getItem("notificationsEnabled");
26-
if (savedPreference === "true") {
37+
if (savedPreference === "true" && ("Notification" in window) && Notification.permission === "granted") {
2738
toggle.checked = true;
39+
} else {
40+
toggle.checked = false;
41+
if (savedPreference === "true") localStorage.setItem("notificationsEnabled", "false");
2842
}
2943

44+
// When user returns to the tab, re-sync in case they revoked permission in browser settings
45+
document.addEventListener("visibilitychange", () => {
46+
if (document.visibilityState === "visible") syncNotificationToggleWithPermission();
47+
});
48+
window.addEventListener("focus", syncNotificationToggleWithPermission);
49+
3050
toggle.addEventListener("change", async (e) => {
3151
const isEnabled = e.target.checked;
3252

3353
// Save preference to localStorage
3454
localStorage.setItem("notificationsEnabled", isEnabled ? "true" : "false");
35-
// When user turns notifications ON, request permission
55+
// When user turns notifications ON: ensure permission is granted or ask for it
3656
if (isEnabled) {
37-
const permission = await requestNotificationPermission();
38-
if (!permission) {
39-
// Keep toggle checked so the checkbox doesn't "flash" back off on mobile.
40-
// Notifications will only fire when permission is granted; tell the user.
57+
if (!("Notification" in window)) {
58+
window.dispatchEvent(new CustomEvent("notificationPermissionDenied"));
59+
return;
60+
}
61+
// Already permitted: no need to ask again, just confirm
62+
if (Notification.permission === "granted") {
63+
window.dispatchEvent(new CustomEvent("notificationPermissionGranted"));
64+
return;
65+
}
66+
// Not yet permitted: ask for permission
67+
const granted = await requestNotificationPermission();
68+
if (granted) {
69+
window.dispatchEvent(new CustomEvent("notificationPermissionGranted"));
70+
} else {
4171
window.dispatchEvent(new CustomEvent("notificationPermissionDenied"));
4272
}
4373
}

0 commit comments

Comments
 (0)