Browse Source

feat: On macOS, Closing Notifications Triggers the 'close' Event (#13306)

* feat(macOS): implement NotificationDismissed() for Cocoa

* feat(macOS): emit NotificationDismissed() when closing "Alert" notifications

* feat(macOS): emit NotificationDismissed() when closing "Banner" notifications

* fix(macos): Remove calls to private APIs from MAS builds (github.com/electron/electron/pull/13306)
Sidney 6 years ago
parent
commit
23541b5b2a

+ 1 - 0
brightray/browser/mac/cocoa_notification.h

@@ -31,6 +31,7 @@ class CocoaNotification : public Notification {
   void NotificationActivated();
   void NotificationActivated(NSUserNotificationAction* action)
     API_AVAILABLE(macosx(10.10));
+  void NotificationDismissed();
 
   NSUserNotification* notification() const { return notification_; }
 

+ 7 - 0
brightray/browser/mac/cocoa_notification.mm

@@ -156,6 +156,13 @@ void CocoaNotification::NotificationActivated(
   this->LogAction("button clicked");
 }
 
+void CocoaNotification::NotificationDismissed() {
+  if (delegate())
+    delegate()->NotificationClosed();
+
+  this->LogAction("dismissed");
+}
+
 void CocoaNotification::LogAction(const char* action) {
   if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
     NSString* identifier = [notification_ valueForKey:@"identifier"];

+ 24 - 0
brightray/browser/mac/notification_center_delegate.mm

@@ -65,4 +65,28 @@
   return YES;
 }
 
+#if !defined(MAS_BUILD)
+// This undocumented method notifies us if a user closes "Alert" notifications
+// https://chromium.googlesource.com/chromium/src/+/lkgr/chrome/browser/notifications/notification_platform_bridge_mac.mm
+- (void)userNotificationCenter:(NSUserNotificationCenter*)center
+               didDismissAlert:(NSUserNotification*)notif {
+  auto* notification = presenter_->GetNotification(notif);
+  if (notification)
+    notification->NotificationDismissed();
+}
+#endif
+
+#if !defined(MAS_BUILD)
+// This undocumented method notifies us if a user closes "Banner" notifications
+// https://github.com/mozilla/gecko-dev/blob/master/widget/cocoa/OSXNotificationCenter.mm
+- (void)userNotificationCenter:(NSUserNotificationCenter*)center
+    didRemoveDeliveredNotifications:(NSArray*)notifications {
+  for (NSUserNotification* notif in notifications) {
+    auto* notification = presenter_->GetNotification(notif);
+    if (notification)
+      notification->NotificationDismissed();
+  }
+}
+#endif
+
 @end