Add support for chat notifications

This commit is contained in:
Charles Gould 2017-02-16 21:10:25 -05:00
parent 48eb68f7b6
commit d734708c71

View File

@ -324,6 +324,11 @@ function main() {
function start() { function start() {
// Request permission to show notifications
Notification.requestPermission().then(function(result) {
console.log('Notification permission: ' + result);
});
// Load initial data // Load initial data
doHttpGet('/games', function(games) { doHttpGet('/games', function(games) {
for (var i = 0; i < games.length; i++) { for (var i = 0; i < games.length; i++) {
@ -352,14 +357,16 @@ function start() {
function addChatAnnouncement(body) { function addChatAnnouncement(body) {
vm.messages.push({ vm.messages.push({
body: body body: body
}) });
showNotification('Announcement', body);
} }
function addChatMessage(sender, body) { function addChatMessage(sender, body) {
vm.messages.push({ vm.messages.push({
sender: sender, sender: sender,
body: body body: body
}) });
showNotification(sender, body);
} }
function doHttpGet(url, callback) { 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(); main();