diff --git a/src/modules/actions/apiUpdaters/chats.ts b/src/modules/actions/apiUpdaters/chats.ts index ada0d3bb7..fc66667d1 100644 --- a/src/modules/actions/apiUpdaters/chats.ts +++ b/src/modules/actions/apiUpdaters/chats.ts @@ -106,12 +106,13 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { return; } - if (update.chatId === currentChatId) { + const isActiveChat = update.chatId === currentChatId; + + if (isActiveChat) { setTimeout(() => { actions.requestChatUpdate({ chatId: update.chatId }); }, CURRENT_CHAT_UNREAD_DELAY); } else { - showNewMessageNotification({ chat, message }); setGlobal(updateChat(global, update.chatId, { unreadCount: chat.unreadCount ? chat.unreadCount + 1 : 1, ...(update.message.hasUnreadMention && { @@ -120,6 +121,8 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { })); } + showNewMessageNotification({ chat, message, isActiveChat }); + break; } diff --git a/src/util/notifications.ts b/src/util/notifications.ts index 2b32bfaf9..c543d54c4 100644 --- a/src/util/notifications.ts +++ b/src/util/notifications.ts @@ -161,8 +161,12 @@ export async function subscribe() { } } -async function checkIfShouldNotify(chat: ApiChat) { +async function checkIfShouldNotify(chat: ApiChat, isActive: boolean) { if (chat.isMuted) return false; + + // Dont show notification for active chat if client has focus + if (isActive && document.hasFocus()) return false; + await getDispatch().loadNotificationsSettings(); const global = getGlobal(); switch (chat.type) { @@ -225,11 +229,12 @@ function getNotificationContent(chat: ApiChat, message: ApiMessage) { export async function showNewMessageNotification({ chat, message, -}: { chat: ApiChat; message: Partial }) { + isActiveChat, +}: { chat: ApiChat; message: Partial; isActiveChat: boolean}) { if (!checkIfNotificationsSupported()) return; if (!message.id) return; - const shouldNotify = await checkIfShouldNotify(chat); + const shouldNotify = await checkIfShouldNotify(chat, isActiveChat); if (!shouldNotify) return; const { @@ -260,6 +265,9 @@ export async function showNewMessageNotification({ chatId: chat.id, messageId: message.id, }); + if (window.focus) { + window.focus(); + } }; }