51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
// 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';
|
|
|
|
function filter(text: string) {
|
|
return (text || '')
|
|
.replace(/&/g, '&')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>');
|
|
}
|
|
|
|
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();
|
|
|
|
const notification = new window.Notification(title, {
|
|
body: OS.isLinux() ? filter(message) : message,
|
|
icon,
|
|
silent:
|
|
silent || audioNotificationSupport !== AudioNotificationSupport.Native,
|
|
});
|
|
notification.onclick = onNotificationClick;
|
|
|
|
if (!silent && audioNotificationSupport === AudioNotificationSupport.Custom) {
|
|
// We kick off the sound to be played. No need to await it.
|
|
new Sound({ src: 'sounds/notification.ogg' }).play();
|
|
}
|
|
|
|
return notification;
|
|
}
|