Add button for requesting notification permission

This commit is contained in:
Jannis Mattheis 2020-02-05 20:06:35 +01:00
parent 0a8fc47e50
commit 3d52a117b1
3 changed files with 24 additions and 3 deletions

View File

@ -14,7 +14,6 @@ import {UserStore} from './user/UserStore';
import {MessagesStore} from './message/MessagesStore'; import {MessagesStore} from './message/MessagesStore';
import {ClientStore} from './client/ClientStore'; import {ClientStore} from './client/ClientStore';
import {PluginStore} from './plugin/PluginStore'; import {PluginStore} from './plugin/PluginStore';
import * as Notifications from './snack/browserNotification';
import {registerReactions} from './reactions'; import {registerReactions} from './reactions';
const defaultDevConfig = { const defaultDevConfig = {
@ -62,7 +61,6 @@ const initStores = (): StoreMapping => {
}; };
(function clientJS() { (function clientJS() {
Notifications.requestPermission();
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
config.set(window.config || defaultProdConfig); config.set(window.config || defaultProdConfig);
} else { } else {

View File

@ -7,6 +7,8 @@ import React, {Component} from 'react';
import {Link} from 'react-router-dom'; import {Link} from 'react-router-dom';
import {observer} from 'mobx-react'; import {observer} from 'mobx-react';
import {inject, Stores} from '../inject'; import {inject, Stores} from '../inject';
import {mayAllowPermission, requestPermission} from '../snack/browserNotification';
import {Button, Typography} from '@material-ui/core';
const styles = (theme: Theme): StyleRules<'drawerPaper' | 'toolbar' | 'link'> => ({ const styles = (theme: Theme): StyleRules<'drawerPaper' | 'toolbar' | 'link'> => ({
drawerPaper: { drawerPaper: {
@ -29,9 +31,15 @@ interface IProps {
} }
@observer @observer
class Navigation extends Component<IProps & Styles & Stores<'appStore'>> { class Navigation extends Component<
IProps & Styles & Stores<'appStore'>,
{showRequestNotification: boolean}
> {
public state = {showRequestNotification: mayAllowPermission()};
public render() { public render() {
const {classes, loggedIn, appStore} = this.props; const {classes, loggedIn, appStore} = this.props;
const {showRequestNotification} = this.state;
const apps = appStore.getItems(); const apps = appStore.getItems();
const userApps = const userApps =
@ -73,6 +81,17 @@ class Navigation extends Component<IProps & Styles & Stores<'appStore'>> {
<Divider /> <Divider />
<div>{loggedIn ? userApps : placeholderItems}</div> <div>{loggedIn ? userApps : placeholderItems}</div>
<Divider /> <Divider />
<Typography align="center" style={{marginTop: 10}}>
{showRequestNotification ? (
<Button
onClick={() => {
requestPermission();
this.setState({showRequestNotification: false});
}}>
Enable Notifications
</Button>
) : null}
</Typography>
</Drawer> </Drawer>
); );
} }

View File

@ -2,6 +2,10 @@ import Notify from 'notifyjs';
import removeMarkdown from 'remove-markdown'; import removeMarkdown from 'remove-markdown';
import {IMessage} from '../types'; import {IMessage} from '../types';
export function mayAllowPermission(): boolean {
return Notify.needsPermission && Notify.isSupported() && Notification.permission !== 'denied';
}
export function requestPermission() { export function requestPermission() {
if (Notify.needsPermission && Notify.isSupported()) { if (Notify.needsPermission && Notify.isSupported()) {
Notify.requestPermission( Notify.requestPermission(