From d734708c71b1eff6f9f051c9919ae4e308fa9909 Mon Sep 17 00:00:00 2001 From: Charles Gould Date: Thu, 16 Feb 2017 21:10:25 -0500 Subject: [PATCH] Add support for chat notifications --- server/src/main/resources/static/client.js | 32 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/server/src/main/resources/static/client.js b/server/src/main/resources/static/client.js index 1e25a22..9dd48e2 100644 --- a/server/src/main/resources/static/client.js +++ b/server/src/main/resources/static/client.js @@ -324,6 +324,11 @@ function main() { function start() { + // Request permission to show notifications + Notification.requestPermission().then(function(result) { + console.log('Notification permission: ' + result); + }); + // Load initial data doHttpGet('/games', function(games) { for (var i = 0; i < games.length; i++) { @@ -352,14 +357,16 @@ function start() { function addChatAnnouncement(body) { vm.messages.push({ body: body - }) + }); + showNotification('Announcement', body); } function addChatMessage(sender, body) { vm.messages.push({ sender: sender, body: body - }) + }); + showNotification(sender, body); } function doHttpGet(url, callback) { @@ -564,4 +571,25 @@ function onUserJoined(message) { } } +function canShowNotification() { + if (document.hidden === 'undefined' || document.hidden === false) { + return false; + } + return Notification.permission === 'granted'; +} + +function showNotification(messageSender, messageBody) { + if (canShowNotification()) { + var title = messageSender; + var options = { + body : messageBody, + icon : '/chat-bubble.png' + }; + var notification = new Notification(title, options); + setTimeout(function() { + notification.close(); + }, 3000); + } +} + main();