ntfy/web/src/app/Notifier.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-06-30 07:57:56 +12:00
import { formatMessage, formatTitleWithDefault, openUrl, playSound, topicDisplayName, topicShortUrl } from "./utils";
2022-03-03 10:16:30 +13:00
import prefs from "./Prefs";
import subscriptionManager from "./SubscriptionManager";
2022-03-10 09:58:21 +13:00
import logo from "../img/ntfy.png";
2022-02-27 04:14:43 +13:00
2022-03-12 09:17:12 +13:00
/**
* The notifier is responsible for displaying desktop notifications. Note that not all modern browsers
* support this; most importantly, all iOS browsers do not support window.Notification.
*/
2022-03-06 18:02:27 +13:00
class Notifier {
async notify(subscriptionId, notification, onClickFallback) {
2022-02-27 04:14:43 +13:00
if (!this.supported()) {
2022-03-06 18:02:27 +13:00
return;
2022-02-27 04:14:43 +13:00
}
2022-03-11 12:11:12 +13:00
const subscription = await subscriptionManager.get(subscriptionId);
const shouldNotify = await this.shouldNotify(subscription, notification);
if (!shouldNotify) {
return;
2022-02-27 04:14:43 +13:00
}
2022-03-03 10:16:30 +13:00
const shortUrl = topicShortUrl(subscription.baseUrl, subscription.topic);
2022-06-30 07:57:56 +12:00
const displayName = topicDisplayName(subscription);
2022-02-27 04:14:43 +13:00
const message = formatMessage(notification);
2022-06-30 07:57:56 +12:00
const title = formatTitleWithDefault(notification, displayName);
2022-02-27 04:14:43 +13:00
2022-03-06 18:02:27 +13:00
// Show notification
2022-02-27 04:14:43 +13:00
console.log(`[Notifier, ${shortUrl}] Displaying notification ${notification.id}: ${message}`);
const n = new Notification(title, {
body: message,
2022-03-11 12:11:12 +13:00
icon: logo,
});
2022-02-27 04:14:43 +13:00
if (notification.click) {
n.onclick = (e) => openUrl(notification.click);
} else {
n.onclick = () => onClickFallback(subscription);
}
// Play sound
2022-03-03 10:16:30 +13:00
const sound = await prefs.sound();
if (sound && sound !== "none") {
try {
await playSound(sound);
2022-03-06 18:02:27 +13:00
} catch (e) {
console.log(`[Notifier, ${shortUrl}] Error playing audio`, e);
}
}
2023-05-24 07:13:01 +12:00
}
2022-03-11 12:11:12 +13:00
granted() {
return this.supported() && Notification.permission === "granted";
}
maybeRequestPermission(cb) {
if (!this.supported()) {
cb(false);
2022-03-11 12:11:12 +13:00
return;
2023-05-24 07:13:01 +12:00
}
2022-03-11 12:11:12 +13:00
if (!this.granted()) {
Notification.requestPermission().then((permission) => {
const granted = permission === "granted";
2022-02-27 04:14:43 +13:00
cb(granted);
2023-05-24 07:13:01 +12:00
});
2022-03-11 12:11:12 +13:00
}
2023-05-24 07:13:01 +12:00
}
async shouldNotify(subscription, notification) {
if (subscription.mutedUntil === 1) {
return false;
}
const priority = notification.priority ? notification.priority : 3;
const minPriority = await prefs.minPriority();
2022-03-03 10:16:30 +13:00
if (priority < minPriority) {
return false;
2023-05-24 07:13:01 +12:00
}
return true;
2023-05-24 07:13:01 +12:00
}
supported() {
return this.browserSupported() && this.contextSupported();
2023-05-24 07:13:01 +12:00
}
browserSupported() {
2022-03-12 09:17:12 +13:00
return "Notification" in window;
2023-05-24 07:13:01 +12:00
}
/**
* Returns true if this is a HTTPS site, or served over localhost. Otherwise the Notification API
* is not supported, see https://developer.mozilla.org/en-US/docs/Web/API/notification
2023-05-24 07:13:01 +12:00
*/
contextSupported() {
return location.protocol === "https:" || location.hostname.match("^127.") || location.hostname === "localhost";
2023-05-24 07:13:01 +12:00
}
2022-02-27 04:14:43 +13:00
}
2022-03-06 18:02:27 +13:00
const notifier = new Notifier();
export default notifier;