Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove same-tag notifications before showing new ones #24406

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions shell/browser/notifications/mac/cocoa_notification.mm
Expand Up @@ -111,8 +111,6 @@

NotificationDismissed();

this->LogAction("dismissed");

notification_.reset(nil);
}

Expand Down Expand Up @@ -163,8 +161,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]
<< ")";
}
Expand Down
7 changes: 5 additions & 2 deletions shell/browser/notifications/notification_presenter.cc
Expand Up @@ -37,8 +37,11 @@ void NotificationPresenter::CloseNotificationWithId(
[&notification_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
11 changes: 11 additions & 0 deletions shell/browser/notifications/platform_notification_service.cc
Expand Up @@ -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(
Expand Down