Enable browser notifications for messaging tools

pull/1/head
yflory 6 years ago
parent 1201f2dcc4
commit c5c6dc8223

@ -12,13 +12,25 @@ define([
} }
}; };
Notifier.notify = function () { Notifier.notify = function (data) {
if (Visible.isSupported() && !Visible.currently()) { if (Visible.isSupported() && !Visible.currently()) {
if (data) {
var title = data.title;
if (document.title) { title += ' (' + document.title + ')'; }
Notify.system(data.msg, title);
return;
}
Notifier.unnotify(); Notifier.unnotify();
notify.tabNotification = Notify.tab(1000, 10); notify.tabNotification = Notify.tab(1000, 10);
} }
}; };
Notifier.getPermission = function () {
if (Notify.isSupported()) {
Notify.getPermission();
}
};
if (Visible.isSupported()) { if (Visible.isSupported()) {
Visible.onChange(function (yes) { Visible.onChange(function (yes) {
if (yes) { Notifier.unnotify(); } if (yes) { Notifier.unnotify(); }

@ -1,6 +1,10 @@
define(['/api/config'], function (ApiConfig) { define(['/api/config'], function (ApiConfig) {
var Module = {}; var Module = {};
var DEFAULT_MAIN = '/customize/main-favicon.png?' + ApiConfig.requireConf.urlArgs;
var DEFAULT_ALT = '/customize/alt-favicon.png?' + ApiConfig.requireConf.urlArgs;
var isSupported = Module.isSupported = function () { var isSupported = Module.isSupported = function () {
return typeof(window.Notification) === 'function'; return typeof(window.Notification) === 'function';
}; };
@ -10,22 +14,28 @@ define(['/api/config'], function (ApiConfig) {
}; };
var getPermission = Module.getPermission = function (f) { var getPermission = Module.getPermission = function (f) {
f = f || function () {};
Notification.requestPermission(function (permission) { Notification.requestPermission(function (permission) {
if (permission === "granted") { f(true); } if (permission === "granted") { f(true); }
else { f(false); } else { f(false); }
}); });
}; };
var create = Module.create = function (msg, title) { var create = Module.create = function (msg, title, icon) {
if (!icon) {
var favicon = document.getElementById('favicon');
icon = favicon.getAttribute('data-main-favicon') || DEFAULT_MAIN;
}
return new Notification(title,{ return new Notification(title,{
// icon: icon, icon: icon,
body: msg, body: msg,
}); });
}; };
Module.system = function (msg, title, icon) { Module.system = function (msg, title, icon) {
// Let's check if the browser supports notifications // Let's check if the browser supports notifications
if (!isSupported()) { console.log("Notifications are not supported"); } if (!isSupported()) { return; /*console.log("Notifications are not supported");*/ }
// Let's check whether notification permissions have already been granted // Let's check whether notification permissions have already been granted
else if (hasPermission()) { else if (hasPermission()) {
@ -41,9 +51,6 @@ define(['/api/config'], function (ApiConfig) {
} }
}; };
var DEFAULT_MAIN = '/customize/main-favicon.png?' + ApiConfig.requireConf.urlArgs;
var DEFAULT_ALT = '/customize/alt-favicon.png?' + ApiConfig.requireConf.urlArgs;
var createFavicon = function () { var createFavicon = function () {
console.log("creating favicon"); console.log("creating favicon");
var fav = document.createElement('link'); var fav = document.createElement('link');

@ -421,8 +421,8 @@ define([
Utils.LocalStore.logout(cb); Utils.LocalStore.logout(cb);
}); });
sframeChan.on('EV_NOTIFY', function () { sframeChan.on('EV_NOTIFY', function (data) {
Notifier.notify(); Notifier.notify(data);
}); });
sframeChan.on('Q_SET_LOGIN_REDIRECT', function (data, cb) { sframeChan.on('Q_SET_LOGIN_REDIRECT', function (data, cb) {
@ -745,6 +745,7 @@ define([
} }
if (cfg.messaging) { if (cfg.messaging) {
Notifier.getPermission();
sframeChan.on('Q_CONTACTS_GET_FRIEND_LIST', function (data, cb) { sframeChan.on('Q_CONTACTS_GET_FRIEND_LIST', function (data, cb) {
Cryptpad.messenger.getFriendList(cb); Cryptpad.messenger.getFriendList(cb);
}); });

@ -189,8 +189,8 @@ define([
ctx.sframeChan.query('Q_LOGOUT', null, cb); ctx.sframeChan.query('Q_LOGOUT', null, cb);
}; };
funcs.notify = function () { funcs.notify = function (data) {
ctx.sframeChan.event('EV_NOTIFY'); ctx.sframeChan.event('EV_NOTIFY', data);
}; };
funcs.setTabTitle = function (newTitle) { funcs.setTabTitle = function (newTitle) {
ctx.sframeChan.event('EV_SET_TAB_TITLE', newTitle); ctx.sframeChan.event('EV_SET_TAB_TITLE', newTitle);

@ -549,6 +549,11 @@ define([
var el_message = markup.message(message); var el_message = markup.message(message);
common.notify(); common.notify();
if (message.type === 'MSG') {
var name = typeof message.name !== "undefined" ? (message.name || Messages.anonymous)
: contactsData[message.author].displayName;
common.notify({title: name, msg: message.text});
}
notifyToolbar(); notifyToolbar();
channel.messages.push(message); channel.messages.push(message);
@ -578,7 +583,7 @@ define([
} }
var lastMsg = channel.messages.slice(-1)[0]; var lastMsg = channel.messages.slice(-1)[0];
if (lastMsg.sig !== channel.HEAD) { if (lastMsg.sig !== channel.HEAD) {
return void notify(chanId); return void notify(chanId, message);
} }
unnotify(chanId); unnotify(chanId);
}); });

Loading…
Cancel
Save