[FIX] mail: discuss notifications small fixes

- discuss notifications are always sent natively if possible
- clicking on native notifications focus the tab
- clicking on native notifications closes them
- granting notification permissions will display a confirmation
  notification
This commit is contained in:
Géry Debongnie
2016-02-05 14:09:19 +01:00
parent ce784ae933
commit 9e31b5de2a
3 changed files with 36 additions and 16 deletions
+29 -14
View File
@@ -51,31 +51,44 @@ bus.on("window_focus", null, function() {
web_client.set_title_part("_chat");
});
// to do: move this to mail.utils
function send_native_notification(title, content) {
var notification = new Notification(title, {body: content, icon: "/mail/static/src/img/odoo_o.png"});
notification.onclick = function (e) {
window.focus();
if (this.cancel) {
this.cancel();
} else if (this.close) {
this.close();
}
};
}
function notify_incoming_message (msg, options) {
if (bus.is_odoo_focused() && options.is_displayed) {
// no need to notify
return;
}
var title = _t('New message');
if (msg.author_id[1]) {
title = _.escape(msg.author_id[1]);
}
var content = parse_and_transform(msg.body, strip_html).substr(0, preview_msg_max_size);
if (bus.is_odoo_focused()) {
if (!options.is_displayed) {
web_client.do_notify(title, content);
}
} else {
if (!bus.is_odoo_focused()) {
global_unread_counter++;
var tab_title = _.str.sprintf(_t("%d Messages"), global_unread_counter);
web_client.set_title_part("_chat", tab_title);
}
if (Notification && Notification.permission === "granted") {
if (bus.is_master) {
new Notification(title, {body: content, icon: "/mail/static/src/img/odoo_o.png", silent: false});
}
} else {
web_client.do_notify(title, content);
if (bus.is_master) {
beep();
}
if (Notification && Notification.permission === "granted") {
if (bus.is_master) {
send_native_notification(title, content);
}
} else {
web_client.do_notify(title, content);
if (bus.is_master) {
beep();
}
}
}
@@ -987,6 +1000,8 @@ var chat_manager = {
return values;
});
},
send_native_notification: send_native_notification,
};
// Initialization
+6 -1
View File
@@ -122,7 +122,12 @@ var ChatAction = Widget.extend(ControlPanelMixin, {
"click .o_mail_request_permission": function (event) {
event.preventDefault();
this.$(".o_mail_annoying_notification_bar").slideUp();
window.Notification.requestPermission();
var def = window.Notification.requestPermission();
if (def) {
def.then(function () {
chat_manager.send_native_notification('Permission granted', 'Odoo has now the permission to send you native notifications on this device.');
});
}
},
"keydown": function (event) {
if (event.which === $.ui.keyCode.ESCAPE && this.selected_message) {
+1 -1
View File
@@ -302,7 +302,7 @@ core.bus.on('web_client_ready', null, function () {
chat_manager.bus.on('anyone_listening', null, function (channel, query) {
_.each(chat_sessions, function (session) {
if (channel.id === session.id && session.window.thread.is_at_bottom()) {
if (channel.id === session.id && session.window.thread.is_at_bottom() && !session.window.is_hidden) {
query.is_displayed = true;
}
});