Notifications: Skip toast and sound when app is focused

This commit is contained in:
Alexander Zinchuk 2021-11-19 17:26:18 +03:00
parent 58d251b67e
commit cc53cbdcb6
2 changed files with 5 additions and 7 deletions

View File

@ -133,7 +133,6 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
notifyAboutNewMessage({ notifyAboutNewMessage({
chat, chat,
message, message,
isActiveChat,
}); });
break; break;

View File

@ -239,15 +239,15 @@ export async function subscribe() {
} }
} }
function checkIfShouldNotify(chat: ApiChat, isActive: boolean) { function checkIfShouldNotify(chat: ApiChat) {
if (!areSettingsLoaded) return false; if (!areSettingsLoaded) return false;
const global = getGlobal(); const global = getGlobal();
const isMuted = selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)); const isMuted = selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global));
if (isMuted || chat.isNotJoined || !chat.isListed) { if (isMuted || chat.isNotJoined || !chat.isListed) {
return false; return false;
} }
// Dont show notification for active chat if client has focus
return !(isActive && document.hasFocus()); return document.hasFocus();
} }
function getNotificationContent(chat: ApiChat, message: ApiMessage) { function getNotificationContent(chat: ApiChat, message: ApiMessage) {
@ -319,10 +319,9 @@ async function getAvatar(chat: ApiChat) {
export async function notifyAboutNewMessage({ export async function notifyAboutNewMessage({
chat, chat,
message, message,
isActiveChat, }: { chat: ApiChat; message: Partial<ApiMessage> }) {
}: { chat: ApiChat; message: Partial<ApiMessage>; isActiveChat: boolean }) {
const { hasWebNotifications } = await loadNotificationSettings(); const { hasWebNotifications } = await loadNotificationSettings();
if (!checkIfShouldNotify(chat, isActiveChat)) return; if (!checkIfShouldNotify(chat)) return;
if (!hasWebNotifications) { if (!hasWebNotifications) {
// only play sound if web notifications are disabled // only play sound if web notifications are disabled
playNotifySoundDebounced(String(message.id) || chat.id); playNotifySoundDebounced(String(message.id) || chat.id);