Use Notifications.getStatus

This commit is contained in:
Daniel Gasienica 2018-05-02 18:04:00 -04:00
parent d5b391757d
commit 1f2a2e1d52

View file

@ -40,38 +40,28 @@
const isAudioNotificationEnabled = const isAudioNotificationEnabled =
storage.get('audio-notification') || false; storage.get('audio-notification') || false;
const isAudioNotificationSupported = Settings.isAudioNotificationSupported(); const isAudioNotificationSupported = Settings.isAudioNotificationSupported();
const shouldPlayNotificationSound =
isAudioNotificationSupported && isAudioNotificationEnabled;
const numNotifications = this.length; const numNotifications = this.length;
console.log('Update notifications:', { const userSetting = this.getUserSetting();
isAppFocused,
const status = Signal.Notifications.getStatus({
isEnabled, isEnabled,
isAppFocused,
isAudioNotificationEnabled,
isAudioNotificationSupported,
numNotifications, numNotifications,
shouldPlayNotificationSound, userSetting,
}); });
if (!isEnabled) { console.log('Update notifications:', status);
if (status.type !== 'ok') {
if (status.shouldClearNotifications) {
this.clear();
}
return; return;
} }
const hasNotifications = numNotifications > 0;
if (!hasNotifications) {
return;
}
const isNotificationOmitted = isAppFocused;
if (isNotificationOmitted) {
this.clear();
return;
}
const setting = this.getSetting();
if (setting === SettingNames.OFF) {
return;
}
drawAttention();
let title; let title;
let message; let message;
let iconUrl; let iconUrl;
@ -86,7 +76,7 @@
].join(' '); ].join(' ');
const last = this.last(); const last = this.last();
switch (setting) { switch (userSetting) {
case SettingNames.COUNT: case SettingNames.COUNT:
title = 'Signal'; title = 'Signal';
message = newMessageCount; message = newMessageCount;
@ -106,10 +96,12 @@
iconUrl = last.get('iconUrl'); iconUrl = last.get('iconUrl');
break; break;
default: default:
console.log(`Error: Unknown setting: '${setting}'`); console.log(`Error: Unknown user setting: '${userSetting}'`);
break; break;
} }
drawAttention();
if (config.polyfillNotifications) { if (config.polyfillNotifications) {
nodeNotifier.notify({ nodeNotifier.notify({
title, title,
@ -124,7 +116,7 @@
body: message, body: message,
icon: iconUrl, icon: iconUrl,
tag: 'signal', tag: 'signal',
silent: !shouldPlayNotificationSound, silent: !status.shouldPlayNotificationSound,
}); });
notification.onclick = this.onClick.bind( notification.onclick = this.onClick.bind(
@ -136,14 +128,14 @@
// We don't want to notify the user about these same messages again // We don't want to notify the user about these same messages again
this.clear(); this.clear();
}, },
getSetting() { getUserSetting() {
return storage.get('notification-setting') || SettingNames.MESSAGE; return storage.get('notification-setting') || SettingNames.MESSAGE;
}, },
onRemove() { onRemove() {
console.log('remove notification'); console.log('Remove notification');
}, },
clear() { clear() {
console.log('remove all notifications'); console.log('Remove all notifications');
this.reset([]); this.reset([]);
}, },
enable() { enable() {