Skip to content

Commit 7628a18

Browse files
committed
[Notification] fallback to balloon notification if toast notification fail
1 parent ec801e3 commit 7628a18

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/nw_notification_manager_toast_win.cc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,28 @@ IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification* /* sender */,
201201
}
202202

203203
// DesktopToastFailedEventHandler
204-
IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification* /* sender */, _In_ IToastFailedEventArgs* /* e */)
204+
IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification* /* sender */, _In_ IToastFailedEventArgs* e)
205205
{
206-
BOOL succeeded = nw::NotificationManager::getSingleton()->DesktopNotificationPostError(_render_process_id, _render_frame_id, _notification_id, L"The toast encountered an error.");
206+
HRESULT errCode;
207+
e->get_ErrorCode(&errCode);
208+
nw::NotificationManagerToastWin* nmtw = static_cast<nw::NotificationManagerToastWin*>(nw::NotificationManager::getSingleton());
209+
std::wstringstream errMsg; errMsg << L"The toast encountered an error code (0x" << std::hex << errCode <<").";
210+
const bool fallBack = errCode == 0x80070490;
211+
if (fallBack)
212+
errMsg << " Fallback to balloon notification!";
213+
214+
BOOL succeeded = nmtw->DesktopNotificationPostError(_render_process_id, _render_frame_id, _notification_id, errMsg.str().c_str());
215+
nmtw->notification_map_.erase(_notification_id);
216+
217+
if (fallBack) {
218+
NotificationManagerToastWin::ForceDisable = true;
219+
delete nmtw;
220+
}
207221
return succeeded ? S_OK : E_FAIL;
208222
}
209223

210224
// ============= NotificationManagerToastWin Implementation =============
225+
bool NotificationManagerToastWin::ForceDisable = false;
211226

212227
HRESULT NotificationManagerToastWin::SetNodeValueString(_In_ HSTRING inputString, _In_ IXmlNode *node, _In_ IXmlDocument *xml)
213228
{
@@ -357,7 +372,7 @@ HRESULT NotificationManagerToastWin::CreateToast(_In_ IToastNotificationManagerS
357372

358373
bool NotificationManagerToastWin::IsSupported() {
359374
static char cachedRes = -1;
360-
375+
if (ForceDisable) return false;
361376
if (cachedRes > -1) return cachedRes;
362377
cachedRes = 0;
363378

src/nw_notification_manager_toast_win.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ namespace nw {
3131
using namespace ABI::Windows::Data::Xml::Dom;
3232

3333
class NotificationManagerToastWin : public NotificationManager{
34+
friend class ToastEventHandler;
3435

3536
ComPtr<IToastNotificationManagerStatics> toastStatics_;
3637
ComPtr<IToastNotifier> notifier_;
3738
std::map<int, ComPtr<IToastNotification>> notification_map_;
39+
static bool ForceDisable;
3840

3941
// internal function for AddDesktopNotification
4042
virtual bool AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,

0 commit comments

Comments
 (0)