signal-desktop/ts/services/notify.ts

52 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { Sound } from '../util/Sound';
import {
AudioNotificationSupport,
getAudioNotificationSupport,
} from '../types/Settings';
import * as OS from '../OS';
2020-06-04 18:16:19 +00:00
function filter(text: string) {
return (text || '')
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
type NotificationType = {
icon: string;
message: string;
onNotificationClick: () => void;
silent: boolean;
title: string;
};
export function notify({
icon,
message,
onNotificationClick,
silent,
title,
}: NotificationType): Notification {
const audioNotificationSupport = getAudioNotificationSupport();
2020-06-04 18:16:19 +00:00
const notification = new window.Notification(title, {
body: OS.isLinux() ? filter(message) : message,
2020-06-04 18:16:19 +00:00
icon,
silent:
silent || audioNotificationSupport !== AudioNotificationSupport.Native,
2020-06-04 18:16:19 +00:00
});
notification.onclick = onNotificationClick;
if (!silent && audioNotificationSupport === AudioNotificationSupport.Custom) {
2021-02-03 16:45:35 +00:00
// We kick off the sound to be played. No need to await it.
new Sound({ src: 'sounds/notification.ogg' }).play();
}
2020-06-04 18:16:19 +00:00
return notification;
}