diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-07 17:18:37 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-07 17:18:37 +1200 |
commit | 4675139fc0dcf2f9f0acc3171af04fba86de0f79 (patch) | |
tree | c6558c96f8bad5f2fe3e71a10b8748ccca3990c2 /modern/src | |
parent | 72aa50650203d6db0b17de59bfd98c9d5b06e3e7 (diff) | |
download | trackermap-web-4675139fc0dcf2f9f0acc3171af04fba86de0f79.tar.gz trackermap-web-4675139fc0dcf2f9f0acc3171af04fba86de0f79.tar.bz2 trackermap-web-4675139fc0dcf2f9f0acc3171af04fba86de0f79.zip |
Implement native notifications
Diffstat (limited to 'modern/src')
-rw-r--r-- | modern/src/SocketContoller.js | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/modern/src/SocketContoller.js b/modern/src/SocketContoller.js index fe686ed8..3cf7feb7 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); + } } } |