Show notification on received message

This commit is contained in:
Jannis Mattheis 2018-03-19 18:54:25 +01:00 committed by Jannis Mattheis
parent 9189045d74
commit 584d28e3fe
2 changed files with 41 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import Layout from './Layout';
import registerServiceWorker from './registerServiceWorker'; import registerServiceWorker from './registerServiceWorker';
import {checkIfAlreadyLoggedIn} from './actions/defaultAxios'; import {checkIfAlreadyLoggedIn} from './actions/defaultAxios';
import config from 'react-global-configuration'; import config from 'react-global-configuration';
import * as Notifications from './stores/Notifications';
import 'typeface-roboto'; import 'typeface-roboto';
import 'typeface-roboto-mono'; import 'typeface-roboto-mono';
@ -21,6 +22,8 @@ const defaultProdConfig = {
}; };
(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

@ -0,0 +1,38 @@
import dispatcher from './dispatcher';
import Notify from 'notifyjs';
export function requestPermission() {
if (Notify.needsPermission && Notify.isSupported()) {
Notify.requestPermission(() => console.log('granted notification permissions'),
() => console.log('notification permission denied'));
}
}
function closeAndFocus(event) {
if (window.parent) {
window.parent.focus();
}
window.focus();
window.location.href = '/';
event.target.close();
}
function closeAfterTimeout(event) {
setTimeout(() => {
event.target.close();
}, 5000);
}
dispatcher.register((data) => {
if (data.type === 'ONE_MESSAGE') {
const msg = data.payload;
const notify = new Notify(msg.title, {
body: msg.message,
icon: '/static/favicon.ico',
notifyClick: closeAndFocus,
notifyShow: closeAfterTimeout,
});
notify.show();
}
});