'use strict'; import React from 'react'; import io from 'socket.io-client'; import { Router, Route, Link } from 'react-router'; import QRCode from 'qrcode.react'; export default class NotifPage extends React.Component { constructor(props) { super(props); this.state = { haveperm: false, connected: false, socket: io.connect() } } componentDidMount() { this.connect(); this.checksupport(); } componentWillUnmount() { this.setState({socket: this.state.socket.removeAllListeners()}); } connect() { let socket = this.state.socket; let room = this.props.urlid; socket.on('connect', () => { socket.emit('room', room); this.setState({connected: true}); socket.on('disconnect', () => { this.setState({connected: false}); }); }); socket.on('message', (data) => { console.log("Notification: " + data); this.checkperm(Notification.permission); this.sendNotification(data); }); } sendNotification(data) { let title = data || 'Received a notification!'; let options = { body: 'Notification from Notica', icon: 'img/icon.png', iconUrl: 'img/icon.png', vibrate: [200, 100, 200] }; try { navigator.serviceWorker.register('/js/sw.js').then((reg) => { reg.showNotification(title, options); }); } catch (e) { // If we are on a browser without serviceWorker new Notification(title, options); } } 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}); } else { this.setState({haveperm: false}); } } render() { let supported = this.state.supported; let haveperm = this.state.haveperm; let connected = this.state.connected; let urlid = this.props.urlid; return (

Notification Page

{ supported ||

This browser does not support desktop notifications.

} { !haveperm && supported &&

Please give this site permission to display notifications.
this.checkperm(Notification.permission)}> Check Again

} { !connected && supported &&

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

} { haveperm && connected && supported &&

This page is monitoring for notifications.

}

Usage

Here are some different ways to use Notica:

Just run it from your terminal:
$ notica

Add a custom message:
$ notica Hello world!

Get an alert when a command finishes:
$ sudo apt-get update; notica Done!

Get an alert when a command succeeds:
$ make all && notica Success!

Get an alert when a command fails:
$ make all || notica Failed!

Tips

Bookmark this page! It is unique to the function in your .bashrc file. Notifications will be sent to all open pages with the same ID code in the URL.

Use quotes on messages with special characters:
$ notica "This is awesome :)"

Need to set Notica up again?
Click here to go back to the instructions.

Open this page on your phone:

); } }