mirror of
https://github.com/getredash/redash.git
synced 2026-03-21 07:00:07 -04:00
Let the notifications go into browser/OS notification trays so users can click on them from there if they miss the initial notification. Modern browsers generally use OS notifications so the user is in control of the notification at the OS level.
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
import { find } from "lodash";
|
|
import debug from "debug";
|
|
import recordEvent from "@/services/recordEvent";
|
|
import redashIconUrl from "@/assets/images/redash_icon_small.png";
|
|
|
|
const logger = debug("redash:notifications");
|
|
|
|
const Notification = window.Notification || null;
|
|
if (!Notification) {
|
|
logger("HTML5 notifications are not supported.");
|
|
}
|
|
|
|
const hidden = find(["hidden", "webkitHidden", "mozHidden", "msHidden"], prop => prop in document);
|
|
|
|
function isPageVisible() {
|
|
return !document[hidden];
|
|
}
|
|
|
|
function getPermissions() {
|
|
if (Notification && Notification.permission === "default") {
|
|
Notification.requestPermission();
|
|
}
|
|
}
|
|
|
|
function showNotification(title, content) {
|
|
if (!Notification || isPageVisible() || Notification.permission !== "granted") {
|
|
return;
|
|
}
|
|
|
|
// using the 'tag' to avoid showing duplicate notifications
|
|
const notification = new Notification(title, {
|
|
tag: title + content,
|
|
body: content,
|
|
icon: redashIconUrl,
|
|
});
|
|
notification.onclick = function onClick() {
|
|
window.focus();
|
|
this.close();
|
|
recordEvent("click", "notification");
|
|
};
|
|
}
|
|
|
|
export default {
|
|
getPermissions,
|
|
showNotification,
|
|
};
|