Notifications: Do not play sound for silent messages (#2064)

This commit is contained in:
Alexander Zinchuk 2022-10-10 14:37:53 +02:00
parent de772bfd59
commit ba622e8f00
4 changed files with 30 additions and 14 deletions

View File

@ -144,7 +144,7 @@ type UniversalMessage = (
& Pick<Partial<GramJs.Message & GramJs.MessageService>, ( & Pick<Partial<GramJs.Message & GramJs.MessageService>, (
'out' | 'message' | 'entities' | 'fromId' | 'peerId' | 'fwdFrom' | 'replyTo' | 'replyMarkup' | 'post' | 'out' | 'message' | 'entities' | 'fromId' | 'peerId' | 'fwdFrom' | 'replyTo' | 'replyMarkup' | 'post' |
'media' | 'action' | 'views' | 'editDate' | 'editHide' | 'mediaUnread' | 'groupedId' | 'mentioned' | 'viaBotId' | 'media' | 'action' | 'views' | 'editDate' | 'editHide' | 'mediaUnread' | 'groupedId' | 'mentioned' | 'viaBotId' |
'replies' | 'fromScheduled' | 'postAuthor' | 'noforwards' | 'reactions' | 'forwards' 'replies' | 'fromScheduled' | 'postAuthor' | 'noforwards' | 'reactions' | 'forwards' | 'silent'
)> )>
); );
@ -181,6 +181,7 @@ export function buildApiMessageWithChatId(chatId: string, mtpMessage: UniversalM
views: mtpMessage.views, views: mtpMessage.views,
forwards: mtpMessage.forwards, forwards: mtpMessage.forwards,
isFromScheduled: mtpMessage.fromScheduled, isFromScheduled: mtpMessage.fromScheduled,
isSilent: mtpMessage.silent,
reactions: mtpMessage.reactions && buildMessageReactions(mtpMessage.reactions), reactions: mtpMessage.reactions && buildMessageReactions(mtpMessage.reactions),
...(replyToMsgId && { replyToMessageId: replyToMsgId }), ...(replyToMsgId && { replyToMessageId: replyToMsgId }),
...(replyToPeerId && { replyToChatId: getApiChatIdFromMtpPeer(replyToPeerId) }), ...(replyToPeerId && { replyToChatId: getApiChatIdFromMtpPeer(replyToPeerId) }),

View File

@ -396,6 +396,7 @@ export interface ApiMessage {
isScheduled?: boolean; isScheduled?: boolean;
shouldHideKeyboardButtons?: boolean; shouldHideKeyboardButtons?: boolean;
isFromScheduled?: boolean; isFromScheduled?: boolean;
isSilent?: boolean;
seenByUserIds?: string[]; seenByUserIds?: string[];
isProtected?: boolean; isProtected?: boolean;
transcriptionId?: string; transcriptionId?: string;

View File

@ -10,6 +10,7 @@ enum Boolean {
type PushData = { type PushData = {
custom: { custom: {
msg_id?: string; msg_id?: string;
silent?: string;
channel_id?: string; channel_id?: string;
chat_id?: string; chat_id?: string;
from_id?: string; from_id?: string;
@ -28,6 +29,7 @@ type NotificationData = {
chatId?: string; chatId?: string;
title: string; title: string;
body: string; body: string;
isSilent?: boolean;
icon?: string; icon?: string;
reaction?: string; reaction?: string;
shouldReplaceHistory?: boolean; shouldReplaceHistory?: boolean;
@ -80,11 +82,17 @@ function getMessageId(data: PushData) {
} }
function getNotificationData(data: PushData): NotificationData { function getNotificationData(data: PushData): NotificationData {
let title = data.title || APP_NAME;
const isSilent = data.custom?.silent === Boolean.True;
if (isSilent) {
title += ' 🔕';
}
return { return {
chatId: getChatId(data), chatId: getChatId(data),
messageId: getMessageId(data), messageId: getMessageId(data),
title: data.title || APP_NAME,
body: data.description, body: data.description,
isSilent,
title,
}; };
} }
@ -113,6 +121,7 @@ function showNotification({
title, title,
icon, icon,
reaction, reaction,
isSilent,
shouldReplaceHistory, shouldReplaceHistory,
}: NotificationData) { }: NotificationData) {
const isFirstBatch = new Date().valueOf() - lastSyncAt < 1000; const isFirstBatch = new Date().valueOf() - lastSyncAt < 1000;
@ -133,8 +142,8 @@ function showNotification({
}; };
return Promise.all([ return Promise.all([
// TODO Remove condition when reaction badges are implemented // TODO Update condition when reaction badges are implemented
!reaction ? playNotificationSound(String(messageId) || chatId || '') : undefined, (!reaction && !isSilent) ? playNotificationSound(String(messageId) || chatId || '') : undefined,
self.registration.showNotification(title, options), self.registration.showNotification(title, options),
]); ]);
} }

View File

@ -321,10 +321,13 @@ function getNotificationContent(chat: ApiChat, message: ApiMessage, reaction?: A
body = 'New message'; body = 'New message';
} }
return { let title = isScreenLocked ? APP_NAME : getChatTitle(getTranslation, chat, privateChatUser);
title: isScreenLocked ? APP_NAME : getChatTitle(getTranslation, chat, privateChatUser),
body, if (message.isSilent) {
}; title += ' 🔕';
}
return { title, body };
} }
async function getAvatar(chat: ApiChat | ApiUser) { async function getAvatar(chat: ApiChat | ApiUser) {
@ -380,10 +383,11 @@ export async function notifyAboutMessage({
if (!checkIfShouldNotify(chat)) return; if (!checkIfShouldNotify(chat)) return;
const areNotificationsSupported = checkIfNotificationsSupported(); const areNotificationsSupported = checkIfNotificationsSupported();
if (!hasWebNotifications || !areNotificationsSupported) { if (!hasWebNotifications || !areNotificationsSupported) {
// Do not play notification sound for reactions if web notifications are disabled if (!message.isSilent && !isReaction) {
if (isReaction) return; // 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); }
return; return;
} }
if (!areNotificationsSupported) return; if (!areNotificationsSupported) return;
@ -413,6 +417,7 @@ export async function notifyAboutMessage({
chatId: chat.id, chatId: chat.id,
messageId: message.id, messageId: message.id,
shouldReplaceHistory: true, shouldReplaceHistory: true,
isSilent: message.isSilent,
reaction: activeReaction?.reaction, reaction: activeReaction?.reaction,
}, },
}); });
@ -446,8 +451,8 @@ export async function notifyAboutMessage({
// Play sound when notification is displayed // Play sound when notification is displayed
notification.onshow = () => { notification.onshow = () => {
// TODO Remove when reaction badges are implemented // TODO Update when reaction badges are implemented
if (isReaction) return; if (isReaction || message.isSilent) return;
playNotifySoundDebounced(String(message.id) || chat.id); playNotifySoundDebounced(String(message.id) || chat.id);
}; };
} }