Skip to content

Instantly share code, notes, and snippets.

@jenyayel
Last active March 1, 2016 21:42
Show Gist options
  • Save jenyayel/5c9e6ad2e86e1b287e80 to your computer and use it in GitHub Desktop.
Save jenyayel/5c9e6ad2e86e1b287e80 to your computer and use it in GitHub Desktop.
_showNotification(client, newState, oldState) {
if(!client.subscribed || newState != SENSOR_STATE_FREE)
return false;
this._clearAllAlertsSubscriptions();
let message = {
title: `Restroom available`,
body: `Restroom just become available at floor ${client.floor.replace('floor-', '')} in ${client.area} wing`,
media: '/media/favicon-160x160.png',
timeout: 30000
};
return this._sendBrowserNotification(message)
.then((wasClicked) => {
this._logger.info(`Notification was sent via NotificationAPI and return result was ${wasClicked}`);
// TODO: do something on click?
})
.catch((reason) => {
this._logger.error(`Failed to use NotificationAPI due to ${reason}`);
setTimeout(() => alert(message.title + '\n' + message.body), 0);
});
}
_sendBrowserNotification(message) {
return new Promise((resolve, reject) => {
if(!browserNotifications.isSupported())
reject('Browser doesn\'t support NotificationAPI');
browserNotifications.requestPermissions()
.then((isPermitted) => {
if(!isPermitted)
reject('User didn\'t give permissions');
browserNotifications
.send(message.title, message.body, message.media, message.timeout)
.then((wasClicked) => resolve(wasClicked))
.catch((error) => reject(error));
})
.catch((error) => reject(error));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment