1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-09 05:14:15 +01:00
ntfy/web/src/app/Notifier.js

83 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-03-06 06:02:27 +01:00
import {formatMessage, formatTitleWithDefault, openUrl, playSound, topicShortUrl} from "./utils";
2022-03-02 22:16:30 +01:00
import prefs from "./Prefs";
import subscriptionManager from "./SubscriptionManager";
2022-03-09 21:58:21 +01:00
import logo from "../img/ntfy.png";
2022-02-26 16:14:43 +01:00
2022-03-11 21:17:12 +01: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 06:02:27 +01:00
class Notifier {
async notify(subscriptionId, notification, onClickFallback) {
2022-03-11 00:11:12 +01:00
if (!this.supported()) {
return;
}
const subscription = await subscriptionManager.get(subscriptionId);
const shouldNotify = await this.shouldNotify(subscription, notification);
if (!shouldNotify) {
return;
}
2022-03-02 22:16:30 +01:00
const shortUrl = topicShortUrl(subscription.baseUrl, subscription.topic);
2022-02-26 16:14:43 +01:00
const message = formatMessage(notification);
2022-03-04 18:10:11 +01:00
const title = formatTitleWithDefault(notification, shortUrl);
2022-03-02 22:16:30 +01:00
2022-03-06 06:02:27 +01:00
// Show notification
console.log(`[Notifier, ${shortUrl}] Displaying notification ${notification.id}: ${message}`);
2022-02-26 16:14:43 +01:00
const n = new Notification(title, {
body: message,
2022-03-09 21:58:21 +01:00
icon: logo
2022-02-26 16:14:43 +01:00
});
if (notification.click) {
n.onclick = (e) => openUrl(notification.click);
2022-02-26 16:14:43 +01:00
} else {
2022-03-08 22:56:41 +01:00
n.onclick = () => onClickFallback(subscription);
2022-02-26 16:14:43 +01:00
}
2022-03-06 06:02:27 +01:00
// Play sound
const sound = await prefs.sound();
if (sound && sound !== "none") {
try {
await playSound(sound);
} catch (e) {
console.log(`[Notifier, ${shortUrl}] Error playing audio`, e);
}
}
2022-02-26 16:14:43 +01:00
}
granted() {
2022-03-11 00:11:12 +01:00
return this.supported() && Notification.permission === 'granted';
2022-02-26 16:14:43 +01:00
}
maybeRequestPermission(cb) {
2022-03-11 00:11:12 +01:00
if (!this.supported()) {
cb(false);
return;
}
2022-02-26 16:14:43 +01:00
if (!this.granted()) {
Notification.requestPermission().then((permission) => {
const granted = permission === 'granted';
cb(granted);
});
}
}
async shouldNotify(subscription, notification) {
2022-03-08 22:56:41 +01:00
if (subscription.mutedUntil === 1) {
return false;
}
const priority = (notification.priority) ? notification.priority : 3;
2022-03-02 22:16:30 +01:00
const minPriority = await prefs.minPriority();
if (priority < minPriority) {
return false;
}
return true;
}
2022-03-11 00:11:12 +01:00
supported() {
return 'Notification' in window;
}
2022-02-26 16:14:43 +01:00
}
2022-03-06 06:02:27 +01:00
const notifier = new Notifier();
export default notifier;