diff --git a/js/notifications.js b/js/notifications.js index ae1a9adde..e27bb910d 100644 --- a/js/notifications.js +++ b/js/notifications.js @@ -42,12 +42,14 @@ const isAudioNotificationSupported = Settings.isAudioNotificationSupported(); const numNotifications = this.length; const userSetting = this.getUserSetting(); + const hasNotificationSupport = !Boolean(config.polyfillNotifications); const status = Signal.Notifications.getStatus({ - isEnabled, + hasNotificationSupport, isAppFocused, isAudioNotificationEnabled, isAudioNotificationSupported, + isEnabled, numNotifications, userSetting, }); @@ -102,16 +104,7 @@ drawAttention(); - if (config.polyfillNotifications) { - nodeNotifier.notify({ - title, - message, - sound: false, - }); - nodeNotifier.on('click', () => { - last.get('conversationId'); - }); - } else { + if (hasNotificationSupport) { const notification = new Notification(title, { body: message, icon: iconUrl, @@ -123,6 +116,15 @@ this, last.get('conversationId') ); + } else { + nodeNotifier.notify({ + title, + message, + sound: false, + }); + nodeNotifier.on('click', () => { + last.get('conversationId'); + }); } // We don't want to notify the user about these same messages again diff --git a/ts/notifications/getStatus.ts b/ts/notifications/getStatus.ts index f23df1485..9f82eacb2 100644 --- a/ts/notifications/getStatus.ts +++ b/ts/notifications/getStatus.ts @@ -4,6 +4,7 @@ interface Environment { isAudioNotificationSupported: boolean; isEnabled: boolean; numNotifications: number; + hasNotificationSupport: boolean; userSetting: UserSetting; } @@ -11,6 +12,7 @@ interface Status { shouldClearNotifications: boolean; shouldPlayNotificationSound: boolean; shouldShowNotifications: boolean; + hasNotificationSupport: boolean; type: Type; } @@ -23,22 +25,30 @@ type Type = | 'noNotifications' | 'userSetting'; -export const getStatus = (environment: Environment): Status => { +export const getStatus = ({ + hasNotificationSupport, + isAppFocused, + isAudioNotificationEnabled, + isAudioNotificationSupported, + isEnabled, + numNotifications, + userSetting, +}: Environment): Status => { const type = ((): Type => { - if (!environment.isEnabled) { + if (!isEnabled) { return 'disabled'; } - const hasNotifications = environment.numNotifications > 0; + const hasNotifications = numNotifications > 0; if (!hasNotifications) { return 'noNotifications'; } - if (environment.isAppFocused) { + if (isAppFocused) { return 'appIsFocused'; } - if (environment.userSetting === 'off') { + if (userSetting === 'off') { return 'userSetting'; } @@ -46,12 +56,14 @@ export const getStatus = (environment: Environment): Status => { })(); const shouldPlayNotificationSound = - environment.isAudioNotificationSupported && - environment.isAudioNotificationEnabled; + isAudioNotificationSupported && + isAudioNotificationEnabled && + hasNotificationSupport; const shouldShowNotifications = type === 'ok'; const shouldClearNotifications = type === 'appIsFocused'; return { + hasNotificationSupport, shouldClearNotifications, shouldPlayNotificationSound, shouldShowNotifications,