Skip to content

Commit 91ce51c

Browse files
fix: remove same-tag notifications before showing new ones (electron#24404)
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
1 parent bf1870f commit 91ce51c

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

shell/browser/notifications/mac/cocoa_notification.mm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@
111111

112112
NotificationDismissed();
113113

114-
this->LogAction("dismissed");
115-
116114
notification_.reset(nil);
117115
}
118116

@@ -163,8 +161,9 @@
163161
}
164162

165163
void CocoaNotification::LogAction(const char* action) {
166-
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
164+
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS") && notification_) {
167165
NSString* identifier = [notification_ valueForKey:@"identifier"];
166+
DCHECK(identifier);
168167
LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String]
169168
<< ")";
170169
}

shell/browser/notifications/notification_presenter.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ void NotificationPresenter::CloseNotificationWithId(
3737
[&notification_id](const Notification* n) {
3838
return n->notification_id() == notification_id;
3939
});
40-
if (it != notifications_.end())
41-
(*it)->Dismiss();
40+
if (it != notifications_.end()) {
41+
Notification* notification = (*it);
42+
notification->Dismiss();
43+
notifications_.erase(notification);
44+
}
4245
}
4346

4447
} // namespace electron

shell/browser/notifications/platform_notification_service.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,19 @@ void PlatformNotificationService::DisplayNotification(
8686
auto* presenter = browser_client_->GetNotificationPresenter();
8787
if (!presenter)
8888
return;
89+
90+
// If a new notification is created with the same tag as an
91+
// existing one, replace the old notification with the new one.
92+
// The notification_id is generated from the tag, so the only way a
93+
// notification will be closed as a result of this call is if one with
94+
// the same tag is already extant.
95+
//
96+
// See: https://notifications.spec.whatwg.org/#showing-a-notification
97+
presenter->CloseNotificationWithId(notification_id);
98+
8999
NotificationDelegateImpl* delegate =
90100
new NotificationDelegateImpl(notification_id);
101+
91102
auto notification = presenter->CreateNotification(delegate, notification_id);
92103
if (notification) {
93104
browser_client_->WebNotificationAllowed(

0 commit comments

Comments
 (0)