-
Notifications
You must be signed in to change notification settings - Fork 14
[OGUI-1854] Add native browser notification helper functions #3257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
graduta
merged 6 commits into
dev
from
feature/FRM/OGUI-1854/native-browser-notification-helpers
Jan 7, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d05ba52
Add native browser notification helper functions
hehoon b13cd14
Use `isContextSecure` from `browserContext.js` in `CopyToClipboardCom…
hehoon 8b3447e
Renamed `BrowserNotification.js` to `browserNotification.js` and move…
hehoon e67dbdc
Add enum `BrowserNotificationPermission` to `browserNotification.js` …
hehoon e352565
Rename `checkBrowserNotificationPermissions` to `areBrowserNotificati…
hehoon 624c822
Merge branch 'dev' into feature/FRM/OGUI-1854/native-browser-notifica…
graduta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2019-2020 CERN and copyright holders of ALICE O2. | ||
| * See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
| * All rights not expressly granted are reserved. | ||
| * | ||
| * This software is distributed under the terms of the GNU General Public | ||
| * License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
| * | ||
| * In applying this license CERN does not waive the privileges and immunities | ||
| * granted to it by virtue of its status as an Intergovernmental Organization | ||
| * or submit itself to any jurisdiction. | ||
| */ | ||
|
|
||
| /* Global: window */ | ||
|
|
||
| /** | ||
| * Checks whether the context is secure (HTTPS) | ||
| * @returns {boolean} Returns `true` if context is secure, `false` otherwise. | ||
| */ | ||
| export const isContextSecure = () => | ||
| window.isSecureContext; |
104 changes: 104 additions & 0 deletions
104
Framework/Frontend/js/src/utilities/browserNotification.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2019-2020 CERN and copyright holders of ALICE O2. | ||
| * See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
| * All rights not expressly granted are reserved. | ||
| * | ||
| * This software is distributed under the terms of the GNU General Public | ||
| * License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
| * | ||
| * In applying this license CERN does not waive the privileges and immunities | ||
| * granted to it by virtue of its status as an Intergovernmental Organization | ||
| * or submit itself to any jurisdiction. | ||
| */ | ||
|
|
||
| /* Global: window */ | ||
|
|
||
| import { isContextSecure } from './browserContext.js'; | ||
|
|
||
| /** | ||
| * Browser notification permission values. | ||
| * Mirrors the Notification API specification. | ||
| * @link https://developer.mozilla.org/en-US/docs/Web/API/Notification/permission_static | ||
| */ | ||
| export const BrowserNotificationPermission = Object.freeze({ | ||
| GRANTED: 'granted', | ||
| DEFAULT: 'default', | ||
| DENIED: 'denied', | ||
| }); | ||
|
|
||
| /** | ||
| * Get the current browser notification permission. | ||
| * @returns {BrowserNotificationPermission|undefined} One of {@link BrowserNotificationPermission}, or `undefined` if unsupported. | ||
| */ | ||
| export const getBrowserNotificationPermission = () => | ||
| window?.Notification?.permission; | ||
|
|
||
| /** | ||
| * Request browser notification permission (async/await). | ||
| * @returns {Promise<BrowserNotificationPermission|undefined>} One of {@link BrowserNotificationPermission}, or `undefined` if unsupported | ||
| */ | ||
| export const requestBrowserNotificationPermissions = async () => { | ||
| if (!isContextSecure()) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const permission = getBrowserNotificationPermission(); | ||
| if (permission === BrowserNotificationPermission.GRANTED) { | ||
| return permission; | ||
| } | ||
|
|
||
| return await window.Notification?.requestPermission(); | ||
| }; | ||
|
|
||
| /** | ||
| * Check if notifications can be shown immediately. | ||
| * @returns {boolean} `true` if the Notification API is available, the context is secure | ||
| * and the current {@link BrowserNotificationPermission} is {@link BrowserNotificationPermission.GRANTED}, `false` otherwise. | ||
| */ | ||
| export const areBrowserNotificationsGranted = () => | ||
| isContextSecure() && getBrowserNotificationPermission() === BrowserNotificationPermission.GRANTED; | ||
|
|
||
| /** | ||
| * @typedef {object} NotificationOptions | ||
| * @property {string} title Notification title | ||
| * @property {string} [body] Notification body | ||
| * @property {string} [icon] Notification icon URL (defaults to server icon) | ||
| * @property {(event: Event) => void} [onclick] Triggered when the notification is clicked | ||
| * @property {(event: Event) => void} [onerror] Triggered if the notification fails to display | ||
| * @property {(event: Event) => void} [onshow] Triggered when the notification is shown | ||
| * @property {(event: Event) => void} [onclose] Triggered when the notification is closed | ||
| */ | ||
|
|
||
| /** | ||
| * Show a native browser notification. | ||
| * @param {NotificationOptions} options - The browser notification options | ||
| * @returns {Notification|null} {@link Notification} instance, or `null` if unavailable | ||
| */ | ||
| export const showNativeBrowserNotification = (options) => { | ||
| if (!areBrowserNotificationsGranted()) { | ||
| return null; | ||
| } | ||
|
|
||
| const { | ||
| title, | ||
| onclick, | ||
| onerror, | ||
| onshow, | ||
| onclose, | ||
| icon = '/favicon.ico', | ||
| ...notificationOptions | ||
| } = options; | ||
| if (!title) { | ||
| return null; | ||
| } | ||
|
|
||
| const notification = new window.Notification(title, { icon, ...notificationOptions }); | ||
| Object.entries({ onclick, onerror, onshow, onclose }) | ||
| .filter(([_, eventFunc]) => typeof eventFunc === 'function') | ||
| .forEach(([eventName, eventFunc]) => { | ||
| notification[eventName] = eventFunc; | ||
| }); | ||
|
|
||
| return notification; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.