diff options
| author | 2026-01-30 02:19:35 +0545 | |
|---|---|---|
| committer | 2026-01-29 21:34:35 +0100 | |
| commit | b59a2101b460200e4276a05f9b8cb4ae7a2e9b62 (patch) | |
| tree | ef2fec4a132bce5c11b35f7285ecf17ef64b09f8 /p | |
| parent | edc750fe444b6af65eccee879a048cf3e0cdd215 (diff) | |
Add option to enable notifications (#8458)
Closes https://github.com/FreshRSS/FreshRSS/issues/7330
- Default behavior is same
- Added FreshRSS_Context::userConf()->html5_disable_notif so that, it determines weather user wants notification. (will not show any even it has permission) (i want default false so disable, so old configs get proper default values)
- Added button such that checking it makes it request permission too
<img width="707" height="119" alt="image" src="https://github.com/user-attachments/assets/a0fdbc4d-9f15-4644-8753-f0e6c979677f" />
- test notification actually happening (how can i trigger it, do i have to wait it), this code fixes permissions.
Diffstat (limited to 'p')
| -rw-r--r-- | p/scripts/extra.js | 32 | ||||
| -rw-r--r-- | p/scripts/main.js | 27 |
2 files changed, 50 insertions, 9 deletions
diff --git a/p/scripts/extra.js b/p/scripts/extra.js index 43bbd89f5..fb1df94db 100644 --- a/p/scripts/extra.js +++ b/p/scripts/extra.js @@ -1,6 +1,6 @@ // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0 'use strict'; -/* globals context, openNotification, xmlHttpRequestJson */ +/* globals context, notifs_html5_is_supported, openNotification, xmlHttpRequestJson */ // <crypto form (Web login)> function poormanSalt() { // If crypto.getRandomValues is not available @@ -581,6 +581,35 @@ function init_user_stats() { document.querySelectorAll('tr[data-need-ajax]').forEach(row => observer.observe(row)); } +function init_enable_notify_button() { + const notify_button = document.getElementById('html5_enable_notif'); + if (!notify_button) return; + // it means unsupported in browser + if (!notifs_html5_is_supported()) { + notify_button.checked = false; + return; + } + + // Not granted, uncheck even if it is saved in server so browser asks for permission + if (Notification.permission !== 'granted') { + notify_button.checked = false; + } + + notify_button.addEventListener('change', async function () { + if (this.checked) { + const permission = await Notification.requestPermission(); + context.notifs_html5_permission = permission; + // Uncheck if user denied + if (permission !== 'granted') { + notify_button.checked = false; + } + } else { + // User disabled notifications + context.notifs_html5_permission = 'denied'; + } + }); +} + function init_extra_afterDOM() { if (!window.context) { if (window.console) { @@ -602,6 +631,7 @@ function init_extra_afterDOM() { init_update_feed(); init_details_attributes(); init_user_stats(); + init_enable_notify_button(); data_auto_leave_validation(document.body); diff --git a/p/scripts/main.js b/p/scripts/main.js index 0ff9e609c..3241c127f 100644 --- a/p/scripts/main.js +++ b/p/scripts/main.js @@ -1965,23 +1965,26 @@ function init_notifications() { // </notification> // <notifs html5> -let notifs_html5_permission = 'denied'; +context.notifs_html5_permission = 'denied'; function notifs_html5_is_supported() { return window.Notification !== undefined; } -function notifs_html5_ask_permission() { +async function notifs_html5_ask_permission() { try { - window.Notification.requestPermission(function () { - notifs_html5_permission = window.Notification.permission; - }); + context.notifs_html5_permission = await window.Notification.requestPermission(); } catch (e) { + // User denied + context.notifs_html5_permission = 'denied'; } } function notifs_html5_show(nb, nb_new) { - if (notifs_html5_permission !== 'granted') { + if (!context.html5_enable_notif) { + return; // from config + } + if (context.notifs_html5_permission !== 'granted') { return; } @@ -2013,8 +2016,16 @@ function init_notifs_html5() { if (!notifs_html5_is_supported()) { return; } - - notifs_html5_permission = notifs_html5_ask_permission(); + // from config, 1st run this should be true + if (!context.html5_enable_notif) { + return; + } + context.notifs_html5_permission = Notification.permission; + // Only ask if the user hasn’t answered yet + // otherwise they need to ask from settings > display + if (context.notifs_html5_permission === 'default') { + notifs_html5_ask_permission(); + } } // </notifs html5> |
