diff --git a/assets/img/icon.png b/assets/img/icon.png new file mode 100644 index 0000000..bb8c07f Binary files /dev/null and b/assets/img/icon.png differ diff --git a/src/ui/NotifPage.js b/src/ui/NotifPage.js index 60a7e2b..64d80d6 100644 --- a/src/ui/NotifPage.js +++ b/src/ui/NotifPage.js @@ -8,16 +8,22 @@ export default class NotifPage extends React.Component { this.state = { haveperm: false, - connected: false + connected: false, + socket: io.connect() } } componentDidMount() { this.connect(); + this.checksupport(); + } + + componentWillUnmount() { + this.setState({socket: this.state.socket.removeAllListeners()}); } connect() { - let socket = io.connect(); + let socket = this.state.socket; let room = this.props.urlid; @@ -31,10 +37,30 @@ export default class NotifPage extends React.Component { }); socket.on('message', (data) => { - new Notification(data); + new Notification( + data || 'Received a notification!', + { + body: 'Notification from Notica', + badge: 'assets/img/icon.png', + icon: 'assets/img/icon.png', + image: 'assets/img/icon.png', + vibrate: 200 + } + ); }); } + checksupport() { + let supported = ('Notification' in window); + this.setState({supported: supported}); + + if (supported) { + Notification.requestPermission(permission => { + this.checkperm(permission); + }.bind(this)); + } + } + checkperm(permission) { if (permission === 'granted') { this.setState({haveperm: true}); @@ -42,13 +68,9 @@ export default class NotifPage extends React.Component { } render() { - let supported = ("Notification" in window); - - if (supported) { - Notification.requestPermission(permission => { - this.checkperm(permission); - }.bind(this)); - } + let supported = this.state.supported; + let haveperm = this.state.haveperm; + let connected = this.state.connected; return (
@@ -58,7 +80,7 @@ export default class NotifPage extends React.Component { { supported ||

This browser does not support desktop notifications.

} - { !this.state.haveperm && supported &&
+ { !haveperm && supported &&

Please give this site permission to display notifications.
@@ -67,14 +89,14 @@ export default class NotifPage extends React.Component {

} - { !this.state.connected && supported &&
+ { !connected && supported &&

Unable to connect to the Notica server.
Attempting to reconnect...

} - { this.state.haveperm && this.state.connected && supported &&

+ { haveperm && connected && supported &&

This page is monitoring for notifications.

}