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({
chat,
message,
isActiveChat,
});
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;
const global = getGlobal();
const isMuted = selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global));
if (isMuted || chat.isNotJoined || !chat.isListed) {
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) {
@ -319,10 +319,9 @@ async function getAvatar(chat: ApiChat) {
export async function notifyAboutNewMessage({
chat,
message,
isActiveChat,
}: { chat: ApiChat; message: Partial<ApiMessage>; isActiveChat: boolean }) {
}: { chat: ApiChat; message: Partial<ApiMessage> }) {
const { hasWebNotifications } = await loadNotificationSettings();
if (!checkIfShouldNotify(chat, isActiveChat)) return;
if (!checkIfShouldNotify(chat)) return;
if (!hasWebNotifications) {
// only play sound if web notifications are disabled
playNotifySoundDebounced(String(message.id) || chat.id);