mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-06-18 10:33:11 +02:00
Support sounds
This commit is contained in:
parent
09b128f27a
commit
dc7ca6e405
13 changed files with 73 additions and 16 deletions
web/src/app
64
web/src/app/Notifier.js
Normal file
64
web/src/app/Notifier.js
Normal file
|
@ -0,0 +1,64 @@
|
|||
import {formatMessage, formatTitleWithDefault, openUrl, playSound, topicShortUrl} from "./utils";
|
||||
import prefs from "./Prefs";
|
||||
import subscriptionManager from "./SubscriptionManager";
|
||||
|
||||
class Notifier {
|
||||
async notify(subscriptionId, notification, onClickFallback) {
|
||||
const subscription = await subscriptionManager.get(subscriptionId);
|
||||
const shouldNotify = await this.shouldNotify(subscription, notification);
|
||||
if (!shouldNotify) {
|
||||
return;
|
||||
}
|
||||
const shortUrl = topicShortUrl(subscription.baseUrl, subscription.topic);
|
||||
const message = formatMessage(notification);
|
||||
const title = formatTitleWithDefault(notification, shortUrl);
|
||||
|
||||
// Show notification
|
||||
console.log(`[Notifier, ${shortUrl}] Displaying notification ${notification.id}: ${message}`);
|
||||
const n = new Notification(title, {
|
||||
body: message,
|
||||
icon: '/static/img/favicon.png'
|
||||
});
|
||||
if (notification.click) {
|
||||
n.onclick = (e) => openUrl(notification.click);
|
||||
} else {
|
||||
n.onclick = onClickFallback;
|
||||
}
|
||||
|
||||
// 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);
|
||||
// FIXME show no sound allowed popup
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
granted() {
|
||||
return Notification.permission === 'granted';
|
||||
}
|
||||
|
||||
maybeRequestPermission(cb) {
|
||||
if (!this.granted()) {
|
||||
Notification.requestPermission().then((permission) => {
|
||||
const granted = permission === 'granted';
|
||||
cb(granted);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async shouldNotify(subscription, notification) {
|
||||
const priority = (notification.priority) ? notification.priority : 3;
|
||||
const minPriority = await prefs.minPriority();
|
||||
if (priority < minPriority) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
const notifier = new Notifier();
|
||||
export default notifier;
|
Loading…
Add table
Add a link
Reference in a new issue