Index: openacs-4/packages/xowiki/www/resources/chat-common.js =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/www/resources/Attic/chat-common.js,v diff -u -r1.10.2.6 -r1.10.2.7 --- openacs-4/packages/xowiki/www/resources/chat-common.js 2 Apr 2020 13:12:54 -0000 1.10.2.6 +++ openacs-4/packages/xowiki/www/resources/chat-common.js 2 Apr 2020 18:09:49 -0000 1.10.2.7 @@ -15,6 +15,53 @@ notifications = 0; } +// Add "Web notifications" of new messages +// https://www.w3.org/TR/notifications/ +function checkNotificationPromise() { + try { + Notification.requestPermission().then(); + } catch(e) { + return false; + } + return true; +} +function askNotificationPermission() { + var notificationBtn = document.getElementById('enableNotifications'); + // function to actually ask the permissions + function handlePermission(permission) { + // Whatever the user answers, we make sure Chrome stores the information + if(!('permission' in Notification)) { + Notification.permission = permission; + } + + // set the button to shown or hidden, depending on what the user answers + if(Notification.permission === 'denied' || Notification.permission === 'default') { + notificationBtn.style.display = 'block'; + } else { + notificationBtn.style.display = 'none'; + } + } + + // Let's check if the browser supports notifications + if (!('Notification' in window)) { + console.log("This browser does not support notifications."); + } else { + if(checkNotificationPromise()) { + Notification.requestPermission() + .then((permission) => { + handlePermission(permission); + }) + } else { + Notification.requestPermission(function(permission) { + handlePermission(permission); + }); + } + } +} +window.onload = function () { + document.getElementById('enableNotifications').addEventListener("click", askNotificationPermission); +} + // Retrieve user_id function chatGetMyUserId() { var my_user = document.getElementById('xowiki-my-user-id'); @@ -51,13 +98,21 @@ // Render the data, being a user or a message function renderData(json) { + var notificationBtn = document.getElementById('enableNotifications'); +notificationBtn.addEventListener("click", askNotificationPermission); + if (json.type == "message") { renderMessage(json); // Produce tab notification if (windowInactive) { notifications++; var newTitle = '(' + notifications + ') ' + title; document.title = newTitle; + // Web notification + var user_id = json.user_id; + var text = json.user.replace(/\\'/g, "\"") + ": " + json.message; + var img = "/shared/portrait-bits.tcl?user_id=" + user_id; + var notification = new Notification(title, { body: text, icon: img }); } } else if (json.type == "users") { renderUsers(json);