Skip to content

Commit

Permalink
Merge pull request oxen-io#2534 from Bilb/add-settings-audio-notifica…
Browse files Browse the repository at this point in the history
…tions

Add support for audio notifications Windows/MacOS/Linux
  • Loading branch information
Bilb authored Oct 11, 2022
2 parents d62a9c1 + 4271e66 commit 74b706d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@
"appearanceSettingsTitle": "Appearance",
"privacySettingsTitle": "Privacy",
"notificationsSettingsTitle": "Notifications",
"audioNotificationsSettingsTitle": "Audio Notifications",
"notificationsSettingsContent": "Notification Content",
"notificationPreview": "Preview",
"recoveryPhraseEmpty": "Enter your recovery phrase",
Expand Down
Binary file added sound/new_message.mp3
Binary file not shown.
27 changes: 23 additions & 4 deletions ts/components/settings/SessionNotificationGroupSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import useUpdate from 'react-use/lib/useUpdate';
import styled from 'styled-components';
import { SettingsKey } from '../../data/settings-key';
import { isAudioNotificationSupported } from '../../types/Settings';
import { Notifications } from '../../util/notifications';
import { SessionButton, SessionButtonColor, SessionButtonType } from '../basic/SessionButton';
import { SessionRadioGroup } from '../basic/SessionRadioGroup';
Expand Down Expand Up @@ -30,10 +31,15 @@ export const SessionNotificationGroupSettings = (props: { hasPassword: boolean |
if (props.hasPassword === null) {
return null;
}
const initialItem =

const initialNotificationEnabled =
window.getSettingValue(SettingsKey.settingsNotification) || NOTIFICATION.MESSAGE;

const notificationsAreEnabled = initialItem && initialItem !== NOTIFICATION.OFF;
const initialAudioNotificationEnabled =
window.getSettingValue(SettingsKey.settingsAudioNotification) || false;

const notificationsAreEnabled =
initialNotificationEnabled && initialNotificationEnabled !== NOTIFICATION.OFF;

const items = [
{
Expand All @@ -58,7 +64,7 @@ export const SessionNotificationGroupSettings = (props: { hasPassword: boolean |
{
conversationId: `preview-notification-${Date.now()}`,
message:
items.find(m => m.value === initialItem)?.label ||
items.find(m => m.value === initialNotificationEnabled)?.label ||
window?.i18n?.('messageBody') ||
'Message body',
title: window.i18n('notificationPreview'),
Expand All @@ -83,14 +89,27 @@ export const SessionNotificationGroupSettings = (props: { hasPassword: boolean |
title={window.i18n('notificationsSettingsTitle')}
active={notificationsAreEnabled}
/>
{notificationsAreEnabled && isAudioNotificationSupported() && (
<SessionToggleWithDescription
onClickToggle={async () => {
await window.setSettingValue(
SettingsKey.settingsAudioNotification,
!initialAudioNotificationEnabled
);
forceUpdate();
}}
title={window.i18n('audioNotificationsSettingsTitle')}
active={initialAudioNotificationEnabled}
/>
)}
{notificationsAreEnabled ? (
<SessionSettingsItemWrapper
title={window.i18n('notificationsSettingsContent')}
description={window.i18n('notificationSettingsDialog')}
inline={false}
>
<SessionRadioGroup
initialItem={initialItem}
initialItem={initialNotificationEnabled}
group={SettingsKey.settingsNotification}
items={items}
onClick={async (selectedRadioValue: string) => {
Expand Down
2 changes: 2 additions & 0 deletions ts/data/settings-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const settingsLinkPreview = 'link-preview-setting';
const settingsStartInTray = 'start-in-tray-setting';
const settingsOpengroupPruning = 'prune-setting';
const settingsNotification = 'notification-setting';
const settingsAudioNotification = 'audio-notification-setting';

export const SettingsKey = {
settingsReadReceipt,
Expand All @@ -19,4 +20,5 @@ export const SettingsKey = {
settingsStartInTray,
settingsOpengroupPruning,
settingsNotification,
settingsAudioNotification,
};
2 changes: 1 addition & 1 deletion ts/test/types/Settings_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Settings', () => {
});

it('should return true', () => {
assert.isFalse(Settings.isAudioNotificationSupported());
assert.isTrue(Settings.isAudioNotificationSupported());
});
});
});
Expand Down
1 change: 1 addition & 0 deletions ts/types/LocalizerKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export type LocalizerKeys =
| 'appMenuUnhide'
| 'timerOption_30_minutes_abbreviated'
| 'pruneSettingDescription'
| 'audioNotificationsSettingsTitle'
| 'voiceMessage'
| 'primaryColorPink'
| 'changePasswordTitle'
Expand Down
3 changes: 2 additions & 1 deletion ts/types/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as OS from '../OS';

const MIN_WINDOWS_VERSION = '8.0.0';

export const isAudioNotificationSupported = () => OS.isWindows(MIN_WINDOWS_VERSION) || OS.isMacOS();
export const isAudioNotificationSupported = () =>
OS.isWindows(MIN_WINDOWS_VERSION) || OS.isMacOS() || OS.isLinux();

// Using `Notification::tag` has a bug on Windows 7:
// https://github.com/electron/electron/issues/11189
Expand Down
14 changes: 12 additions & 2 deletions ts/util/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { debounce, last } from 'lodash';
import { SettingsKey } from '../data/settings-key';
import { getStatus } from '../notifications';
import { UserSetting } from '../notifications/getStatus';
import { isMacOS } from '../OS';
Expand All @@ -21,6 +22,8 @@ function filter(text?: string) {
.replace(/>/g, '&gt;');
}

let sound: any;

export type SessionNotification = {
conversationId: string;
iconUrl: string | null;
Expand Down Expand Up @@ -117,7 +120,8 @@ function update(forceRefresh = false) {
}

const isAppFocused = isWindowFocused();
const isAudioNotificationEnabled = (Storage.get('audio-notification') as boolean) || false;
const isAudioNotificationEnabled =
(Storage.get(SettingsKey.settingsAudioNotification) as boolean) || false;
const audioNotificationSupported = isAudioNotificationSupported();
// const isNotificationGroupingSupported = Settings.isNotificationGroupingSupported();
const numNotifications = currentNotifications.length;
Expand Down Expand Up @@ -216,10 +220,16 @@ function update(forceRefresh = false) {
}

window.drawAttention();
if (status.shouldPlayNotificationSound) {
if (!sound) {
sound = new Audio('sound/new_message.mp3');
}
void sound.play();
}
lastNotificationDisplayed = new Notification(title || '', {
body: window.platform === 'linux' ? filter(message) : message,
icon: iconUrl || undefined,
silent: !status.shouldPlayNotificationSound,
silent: true,
});
lastNotificationDisplayed.onclick = () => {
window.openFromNotification(lastNotification.conversationId);
Expand Down

0 comments on commit 74b706d

Please sign in to comment.