Skip to content

Commit

Permalink
[FIX] mail: better push notifications error handling
Browse files Browse the repository at this point in the history
A known issue in Brave (brave/brave-browser#2362 (comment))
prevents push notifications unless "Use Google Services for Push Messaging" is enabled in settings.

Before this commit, attempting to enable desktop notifications in Brave resulted in a traceback.

This commit handles the failure by displaying an error message and suggesting enabling
Google Services when using Brave.

task-4191214

closes #184394

X-original-commit: 491889c
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
  • Loading branch information
Atovange committed Oct 23, 2024
1 parent 44bdcec commit 1f81afa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
16 changes: 16 additions & 0 deletions addons/mail/i18n/mail.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,15 @@ msgstr ""
msgid "Bounced"
msgstr ""

#. module: mail
#. odoo-javascript
#: code:addons/mail/static/src/webclient/web/webclient.js:0
#, python-format
msgid ""
"Brave: enable 'Google Services for Push Messaging' to enable push "
"notifications"
msgstr ""

#. module: mail
#. odoo-javascript
#: code:addons/mail/static/src/discuss/call/common/call_settings.xml:0
Expand Down Expand Up @@ -3375,6 +3384,13 @@ msgstr ""
msgid "Failed"
msgstr ""

#. module: mail
#. odoo-javascript
#: code:addons/mail/static/src/webclient/web/webclient.js:0
#, python-format
msgid "Failed to enable push notifications"
msgstr ""

#. module: mail
#. odoo-javascript
#: code:addons/mail/static/src/discuss/gif_picker/common/gif_picker.xml:0
Expand Down
31 changes: 27 additions & 4 deletions addons/mail/static/src/webclient/web/webclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useService } from "@web/core/utils/hooks";
import { patch } from "@web/core/utils/patch";
import { WebClient } from "@web/webclient/webclient";
import { onWillDestroy } from "@odoo/owl";
import { _t } from "@web/core/l10n/translation";

const USER_DEVICES_MODEL = "mail.push.device";

Expand All @@ -13,6 +14,7 @@ patch(WebClient.prototype, {
setup() {
super.setup();
this.orm = useService("orm");
this.notification = useService("notification");
if (this._canSendNativeNotification) {
this._subscribePush();
}
Expand Down Expand Up @@ -59,10 +61,31 @@ patch(WebClient.prototype, {
// This may occur if the subscription was refreshed by the browser,
// but it may also happen if the subscription has been revoked or lost.
if (!subscription) {
subscription = await pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: await this._getApplicationServerKey(),
});
try {
subscription = await pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: await this._getApplicationServerKey(),
});
} catch (error) {
console.warn(error);
this.notification.add(error.message, {
title: _t("Failed to enable push notifications"),
type: "danger",
sticky: true,
});
if (await navigator.brave?.isBrave()) {
this.notification.add(
_t(
"Brave: enable 'Google Services for Push Messaging' to enable push notifications"
),
{
type: "warning",
sticky: true,
}
);
}
return;
}
browser.localStorage.setItem(`${USER_DEVICES_MODEL}_endpoint`, subscription.endpoint);
}
const kwargs = subscription.toJSON();
Expand Down

0 comments on commit 1f81afa

Please sign in to comment.