From 91ce7de204598cdefbcbf8cd3d09e2470522eef5 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 1 Jul 2020 20:07:02 -0700 Subject: [PATCH] fix: remove same-tag notifications before showing new ones --- .../notifications/mac/cocoa_notification.mm | 14 ++++++++------ .../notifications/notification_presenter.cc | 7 +++++-- .../notifications/platform_notification_service.cc | 11 +++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/shell/browser/notifications/mac/cocoa_notification.mm b/shell/browser/notifications/mac/cocoa_notification.mm index b4be60027323f..96c3a5ae5de24 100644 --- a/shell/browser/notifications/mac/cocoa_notification.mm +++ b/shell/browser/notifications/mac/cocoa_notification.mm @@ -105,15 +105,16 @@ } void CocoaNotification::Dismiss() { - if (notification_) + NotificationDismissed(); + + if (notification_) { [NSUserNotificationCenter.defaultUserNotificationCenter removeDeliveredNotification:notification_]; - NotificationDismissed(); - - this->LogAction("dismissed"); + this->LogAction("dismissed"); - notification_.reset(nil); + notification_.reset(nil); + } } void CocoaNotification::NotificationDisplayed() { @@ -163,8 +164,9 @@ } void CocoaNotification::LogAction(const char* action) { - if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) { + if (getenv("ELECTRON_DEBUG_NOTIFICATIONS") && notification_) { NSString* identifier = [notification_ valueForKey:@"identifier"]; + DCHECK(identifier); LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String] << ")"; } diff --git a/shell/browser/notifications/notification_presenter.cc b/shell/browser/notifications/notification_presenter.cc index 891bd171f125b..4aef716a72a8b 100644 --- a/shell/browser/notifications/notification_presenter.cc +++ b/shell/browser/notifications/notification_presenter.cc @@ -37,8 +37,11 @@ void NotificationPresenter::CloseNotificationWithId( [¬ification_id](const Notification* n) { return n->notification_id() == notification_id; }); - if (it != notifications_.end()) - (*it)->Dismiss(); + if (it != notifications_.end()) { + Notification* notification = (*it); + notification->Dismiss(); + notifications_.erase(notification); + } } } // namespace electron diff --git a/shell/browser/notifications/platform_notification_service.cc b/shell/browser/notifications/platform_notification_service.cc index f6f15bfdb542c..08b1e422f9354 100644 --- a/shell/browser/notifications/platform_notification_service.cc +++ b/shell/browser/notifications/platform_notification_service.cc @@ -86,8 +86,19 @@ void PlatformNotificationService::DisplayNotification( auto* presenter = browser_client_->GetNotificationPresenter(); if (!presenter) return; + + // If a new notification is created with the same tag as an + // existing one, replace the old notification with the new one. + // The notification_id is generated from the tag, so the only way a + // notification will be closed as a result of this call is if one with + // the same tag is already extant. + // + // See: https://notifications.spec.whatwg.org/#showing-a-notification + presenter->CloseNotificationWithId(notification_id); + NotificationDelegateImpl* delegate = new NotificationDelegateImpl(notification_id); + auto notification = presenter->CreateNotification(delegate, notification_id); if (notification) { browser_client_->WebNotificationAllowed(