diff --git a/shell/browser/notifications/mac/cocoa_notification.mm b/shell/browser/notifications/mac/cocoa_notification.mm index b4be60027323f..6e18a30f1fd23 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_) + if (notification_) { [NSUserNotificationCenter.defaultUserNotificationCenter removeDeliveredNotification:notification_]; - NotificationDismissed(); + NotificationDismissed(); - this->LogAction("dismissed"); + this->LogAction("dismissed"); - notification_.reset(nil); + notification_.reset(nil); + } } void CocoaNotification::NotificationDisplayed() { @@ -165,8 +166,10 @@ void CocoaNotification::LogAction(const char* action) { if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) { NSString* identifier = [notification_ valueForKey:@"identifier"]; - LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String] - << ")"; + if (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..7d7da21bb8458 100644 --- a/shell/browser/notifications/notification_presenter.cc +++ b/shell/browser/notifications/notification_presenter.cc @@ -23,7 +23,9 @@ base::WeakPtr NotificationPresenter::CreateNotification( Notification* notification = CreateNotificationObject(delegate); notification->set_notification_id(notification_id); notifications_.insert(notification); - return notification->GetWeakPtr(); + + last_notification_ = notification->GetWeakPtr(); + return last_notification_; } void NotificationPresenter::RemoveNotification(Notification* notification) { diff --git a/shell/browser/notifications/notification_presenter.h b/shell/browser/notifications/notification_presenter.h index 703810c117011..005fd6ce25767 100644 --- a/shell/browser/notifications/notification_presenter.h +++ b/shell/browser/notifications/notification_presenter.h @@ -28,6 +28,8 @@ class NotificationPresenter { std::set notifications() const { return notifications_; } + base::WeakPtr last_notification() { return last_notification_; } + protected: NotificationPresenter(); virtual Notification* CreateNotificationObject( @@ -40,6 +42,8 @@ class NotificationPresenter { std::set notifications_; + base::WeakPtr last_notification_; + DISALLOW_COPY_AND_ASSIGN(NotificationPresenter); }; diff --git a/shell/browser/notifications/platform_notification_service.cc b/shell/browser/notifications/platform_notification_service.cc index f6f15bfdb542c..3b8e9a99f3e57 100644 --- a/shell/browser/notifications/platform_notification_service.cc +++ b/shell/browser/notifications/platform_notification_service.cc @@ -86,6 +86,12 @@ void PlatformNotificationService::DisplayNotification( auto* presenter = browser_client_->GetNotificationPresenter(); if (!presenter) return; + + // Ensure the last notification is removed before presenting a new one. + auto last_notification = presenter->last_notification(); + if (last_notification) + last_notification->Dismiss(); + NotificationDelegateImpl* delegate = new NotificationDelegateImpl(notification_id); auto notification = presenter->CreateNotification(delegate, notification_id);