Prime message audio and silence duplicate OS notification sound
This commit is contained in:
@@ -95,10 +95,10 @@ export function notifyIncomingMessage(topicName: string, msg: StoredMessage, con
|
||||
/** Shared by both real call sites below -- a new message, or being newly added to a group. `tag:
|
||||
* topicName` makes a second notification from the same conversation REPLACE the first in the OS
|
||||
* notification tray instead of stacking, matching every mainstream IM web client. */
|
||||
function showNotification(topicName: string, title: string, body: string) {
|
||||
function showNotification(topicName: string, title: string, body: string, silent = false) {
|
||||
let notif: Notification;
|
||||
try {
|
||||
notif = new Notification(title, { body, tag: topicName, icon: '/app-icon.png' });
|
||||
notif = new Notification(title, { body, tag: topicName, icon: '/app-icon.png', ...(silent ? { silent: true } : {}) });
|
||||
} catch {
|
||||
// Some browsers/contexts throw constructing Notification even with permission granted (e.g. an
|
||||
// OS-level notification setting disabled after the fact) -- never let this crash the message
|
||||
@@ -116,7 +116,7 @@ function fireNotification(topicName: string, msg: StoredMessage, contact?: Conta
|
||||
const title = contact?.public?.fn || topicName;
|
||||
const myUserId = useSessionStore.getState().myUserId;
|
||||
const body = formatMessagePreview(msg, myUserId) || '发来一条新消息';
|
||||
showNotification(topicName, title, body);
|
||||
showNotification(topicName, title, body, true);
|
||||
}
|
||||
|
||||
/** "被拉入群时title的消息没有提示" -- topics.ts's reconcileMeSubs() is the only thing that can even
|
||||
|
||||
@@ -1,4 +1,27 @@
|
||||
let audio: HTMLAudioElement | null = null;
|
||||
let priming = false;
|
||||
|
||||
async function primeSound(): Promise<void> {
|
||||
if (priming) return;
|
||||
priming = true;
|
||||
try {
|
||||
audio ??= new Audio('/message-notification.mp3');
|
||||
audio.muted = true;
|
||||
await audio.play();
|
||||
audio.pause();
|
||||
audio.currentTime = 0;
|
||||
audio.muted = false;
|
||||
window.removeEventListener('pointerdown', primeSound);
|
||||
window.removeEventListener('keydown', primeSound);
|
||||
} catch {
|
||||
try { if (audio) audio.muted = false; } catch { /* Audio may be unavailable. */ }
|
||||
} finally {
|
||||
priming = false;
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('pointerdown', primeSound);
|
||||
window.addEventListener('keydown', primeSound);
|
||||
|
||||
export function playMessageNotificationSound(): void {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user