Add Desktop notifications (#252)

* Add notifications

* Abide push actions

* Handle browsers not having notification support

* Ask for notification permission after loading

* Make usePermission work without live permission support

* Focus message when clicking the notification

* make const all caps

* Fix usePermission error in Safari

* Fix live permissions

* Remove userActivity and use document.visibilityState instead

* Change setting label to "desktop notifications"

* Check for notification permissions in the settings.js
This commit is contained in:
ginnyTheCat
2022-01-29 15:20:51 +01:00
committed by GitHub
parent d0b4e092b3
commit c828dfd596
7 changed files with 174 additions and 15 deletions

View File

@@ -28,6 +28,7 @@ class Settings extends EventEmitter {
this.isPeopleDrawer = this.getIsPeopleDrawer();
this.hideMembershipEvents = this.getHideMembershipEvents();
this.hideNickAvatarEvents = this.getHideNickAvatarEvents();
this._showNotifications = this.getShowNotifications();
this.isTouchScreenDevice = ('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0);
}
@@ -110,6 +111,20 @@ class Settings extends EventEmitter {
return settings.isPeopleDrawer;
}
get showNotifications() {
if (window.Notification?.permission !== 'granted') return false;
return this._showNotifications;
}
getShowNotifications() {
if (typeof this._showNotifications === 'boolean') return this._showNotifications;
const settings = getSettings();
if (settings === null) return true;
if (typeof settings.showNotifications === 'undefined') return true;
return settings.showNotifications;
}
setter(action) {
const actions = {
[cons.actions.settings.TOGGLE_SYSTEM_THEME]: () => {
@@ -140,6 +155,15 @@ class Settings extends EventEmitter {
setSettings('hideNickAvatarEvents', this.hideNickAvatarEvents);
this.emit(cons.events.settings.NICKAVATAR_EVENTS_TOGGLED, this.hideNickAvatarEvents);
},
[cons.actions.settings.TOGGLE_NOTIFICATIONS]: async () => {
if (window.Notification?.permission !== 'granted') {
this._showNotifications = false;
} else {
this._showNotifications = !this._showNotifications;
}
setSettings('showNotifications', this._showNotifications);
this.emit(cons.events.settings.NOTIFICATIONS_TOGGLED, this._showNotifications);
},
};
actions[action.type]?.();