Skip to content

Commit

Permalink
fix: remove notifications before showing new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jun 25, 2020
1 parent 4290555 commit d681e19
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
15 changes: 9 additions & 6 deletions shell/browser/notifications/mac/cocoa_notification.mm
Expand Up @@ -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() {
Expand Down Expand Up @@ -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]
<< ")";
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion shell/browser/notifications/notification_presenter.cc
Expand Up @@ -23,7 +23,9 @@ base::WeakPtr<Notification> 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) {
Expand Down
4 changes: 4 additions & 0 deletions shell/browser/notifications/notification_presenter.h
Expand Up @@ -28,6 +28,8 @@ class NotificationPresenter {

std::set<Notification*> notifications() const { return notifications_; }

base::WeakPtr<Notification> last_notification() { return last_notification_; }

protected:
NotificationPresenter();
virtual Notification* CreateNotificationObject(
Expand All @@ -40,6 +42,8 @@ class NotificationPresenter {

std::set<Notification*> notifications_;

base::WeakPtr<Notification> last_notification_;

DISALLOW_COPY_AND_ASSIGN(NotificationPresenter);
};

Expand Down
6 changes: 6 additions & 0 deletions shell/browser/notifications/platform_notification_service.cc
Expand Up @@ -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);
Expand Down

0 comments on commit d681e19

Please sign in to comment.