fix: remove same-tag notifications before showing new ones (#24302)

This commit is contained in:
Shelley Vohr 2020-07-02 23:04:32 -07:00 committed by GitHub
parent 2a6d6d6ea7
commit fd0eaf4507
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View file

@ -111,8 +111,6 @@ void CocoaNotification::Dismiss() {
NotificationDismissed(); NotificationDismissed();
this->LogAction("dismissed");
notification_.reset(nil); notification_.reset(nil);
} }
@ -163,8 +161,9 @@ void CocoaNotification::NotificationDismissed() {
} }
void CocoaNotification::LogAction(const char* action) { void CocoaNotification::LogAction(const char* action) {
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) { if (getenv("ELECTRON_DEBUG_NOTIFICATIONS") && notification_) {
NSString* identifier = [notification_ valueForKey:@"identifier"]; NSString* identifier = [notification_ valueForKey:@"identifier"];
DCHECK(identifier);
LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String] LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String]
<< ")"; << ")";
} }

View file

@ -37,8 +37,11 @@ void NotificationPresenter::CloseNotificationWithId(
[&notification_id](const Notification* n) { [&notification_id](const Notification* n) {
return n->notification_id() == notification_id; return n->notification_id() == notification_id;
}); });
if (it != notifications_.end()) if (it != notifications_.end()) {
(*it)->Dismiss(); Notification* notification = (*it);
notification->Dismiss();
notifications_.erase(notification);
}
} }
} // namespace electron } // namespace electron

View file

@ -86,8 +86,19 @@ void PlatformNotificationService::DisplayNotification(
auto* presenter = browser_client_->GetNotificationPresenter(); auto* presenter = browser_client_->GetNotificationPresenter();
if (!presenter) if (!presenter)
return; 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 = NotificationDelegateImpl* delegate =
new NotificationDelegateImpl(notification_id); new NotificationDelegateImpl(notification_id);
auto notification = presenter->CreateNotification(delegate, notification_id); auto notification = presenter->CreateNotification(delegate, notification_id);
if (notification) { if (notification) {
browser_client_->WebNotificationAllowed( browser_client_->WebNotificationAllowed(