From 4675139fc0dcf2f9f0acc3171af04fba86de0f79 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 7 Sep 2018 17:18:37 +1200 Subject: Implement native notifications --- modern/src/SocketContoller.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'modern') diff --git a/modern/src/SocketContoller.js b/modern/src/SocketContoller.js index fe686ed..3cf7feb 100644 --- a/modern/src/SocketContoller.js +++ b/modern/src/SocketContoller.js @@ -2,6 +2,23 @@ import { Component } from 'react'; import { connect } from 'react-redux'; import { updatePositions } from './actions'; +const displayNotifications = events => { + if ("Notification" in window) { + if (Notification.permission === "granted") { + for (const event of events) { + const notification = new Notification(`Event: ${event.type}`); + setTimeout(notification.close.bind(notification), 4 * 1000); + } + } else if (Notification.permission !== "denied") { + Notification.requestPermission(permission => { + if (permission === "granted") { + displayNotifications(events); + } + }); + } + } +}; + class SocketController extends Component { connectSocket() { const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; @@ -16,6 +33,9 @@ class SocketController extends Component { if (data.positions) { this.props.dispatch(updatePositions(data.positions)); } + if (data.events) { + displayNotifications(data.events); + } } } -- cgit v1.2.3