Enable browser notifications for messaging tools
parent
1201f2dcc4
commit
c5c6dc8223
|
@ -12,13 +12,25 @@ define([
|
|||
}
|
||||
};
|
||||
|
||||
Notifier.notify = function () {
|
||||
Notifier.notify = function (data) {
|
||||
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();
|
||||
notify.tabNotification = Notify.tab(1000, 10);
|
||||
}
|
||||
};
|
||||
|
||||
Notifier.getPermission = function () {
|
||||
if (Notify.isSupported()) {
|
||||
Notify.getPermission();
|
||||
}
|
||||
};
|
||||
|
||||
if (Visible.isSupported()) {
|
||||
Visible.onChange(function (yes) {
|
||||
if (yes) { Notifier.unnotify(); }
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
define(['/api/config'], function (ApiConfig) {
|
||||
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 () {
|
||||
return typeof(window.Notification) === 'function';
|
||||
};
|
||||
|
@ -10,22 +14,28 @@ define(['/api/config'], function (ApiConfig) {
|
|||
};
|
||||
|
||||
var getPermission = Module.getPermission = function (f) {
|
||||
f = f || function () {};
|
||||
Notification.requestPermission(function (permission) {
|
||||
if (permission === "granted") { f(true); }
|
||||
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,{
|
||||
// icon: icon,
|
||||
icon: icon,
|
||||
body: msg,
|
||||
});
|
||||
};
|
||||
|
||||
Module.system = function (msg, title, icon) {
|
||||
// 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
|
||||
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 () {
|
||||
console.log("creating favicon");
|
||||
var fav = document.createElement('link');
|
||||
|
@ -112,4 +119,4 @@ define(['/api/config'], function (ApiConfig) {
|
|||
};
|
||||
|
||||
return Module;
|
||||
});
|
||||
});
|
||||
|
|
|
@ -421,8 +421,8 @@ define([
|
|||
Utils.LocalStore.logout(cb);
|
||||
});
|
||||
|
||||
sframeChan.on('EV_NOTIFY', function () {
|
||||
Notifier.notify();
|
||||
sframeChan.on('EV_NOTIFY', function (data) {
|
||||
Notifier.notify(data);
|
||||
});
|
||||
|
||||
sframeChan.on('Q_SET_LOGIN_REDIRECT', function (data, cb) {
|
||||
|
@ -745,6 +745,7 @@ define([
|
|||
}
|
||||
|
||||
if (cfg.messaging) {
|
||||
Notifier.getPermission();
|
||||
sframeChan.on('Q_CONTACTS_GET_FRIEND_LIST', function (data, cb) {
|
||||
Cryptpad.messenger.getFriendList(cb);
|
||||
});
|
||||
|
|
|
@ -189,8 +189,8 @@ define([
|
|||
ctx.sframeChan.query('Q_LOGOUT', null, cb);
|
||||
};
|
||||
|
||||
funcs.notify = function () {
|
||||
ctx.sframeChan.event('EV_NOTIFY');
|
||||
funcs.notify = function (data) {
|
||||
ctx.sframeChan.event('EV_NOTIFY', data);
|
||||
};
|
||||
funcs.setTabTitle = function (newTitle) {
|
||||
ctx.sframeChan.event('EV_SET_TAB_TITLE', newTitle);
|
||||
|
|
|
@ -549,6 +549,11 @@ define([
|
|||
var el_message = markup.message(message);
|
||||
|
||||
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();
|
||||
|
||||
channel.messages.push(message);
|
||||
|
@ -578,7 +583,7 @@ define([
|
|||
}
|
||||
var lastMsg = channel.messages.slice(-1)[0];
|
||||
if (lastMsg.sig !== channel.HEAD) {
|
||||
return void notify(chanId);
|
||||
return void notify(chanId, message);
|
||||
}
|
||||
unnotify(chanId);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue