notification_center_delegate.mm 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/notifications/mac/notification_center_delegate.h"
  5. #include <string>
  6. #include "base/logging.h"
  7. #include "electron/mas.h"
  8. #include "shell/browser/notifications/mac/cocoa_notification.h"
  9. #include "shell/browser/notifications/mac/notification_presenter_mac.h"
  10. // NSUserNotification is deprecated; we need to use the
  11. // UserNotifications.frameworks API instead
  12. #pragma clang diagnostic push
  13. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  14. @implementation NotificationCenterDelegate
  15. - (instancetype)initWithPresenter:
  16. (electron::NotificationPresenterMac*)presenter {
  17. self = [super init];
  18. if (!self)
  19. return nil;
  20. presenter_ = presenter;
  21. return self;
  22. }
  23. - (void)userNotificationCenter:(NSUserNotificationCenter*)center
  24. didDeliverNotification:(NSUserNotification*)notif {
  25. auto* notification = presenter_->GetNotification(notif);
  26. if (notification)
  27. notification->NotificationDisplayed();
  28. }
  29. - (void)userNotificationCenter:(NSUserNotificationCenter*)center
  30. didActivateNotification:(NSUserNotification*)notif {
  31. auto* notification = presenter_->GetNotification(notif);
  32. if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
  33. LOG(INFO) << "Notification activated (" << [notif.identifier UTF8String]
  34. << ")";
  35. }
  36. if (notification) {
  37. // Ref:
  38. // https://developer.apple.com/documentation/foundation/nsusernotificationactivationtype?language=objc
  39. if (notif.activationType ==
  40. NSUserNotificationActivationTypeContentsClicked) {
  41. notification->NotificationClicked();
  42. } else if (notif.activationType ==
  43. NSUserNotificationActivationTypeActionButtonClicked) {
  44. notification->NotificationActivated();
  45. } else if (notif.activationType ==
  46. NSUserNotificationActivationTypeReplied) {
  47. notification->NotificationReplied([notif.response.string UTF8String]);
  48. } else {
  49. if (notif.activationType ==
  50. NSUserNotificationActivationTypeAdditionalActionClicked) {
  51. notification->NotificationActivated([notif additionalActivationAction]);
  52. }
  53. }
  54. }
  55. }
  56. - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center
  57. shouldPresentNotification:(NSUserNotification*)notification {
  58. // Display notifications even if the app is active.
  59. return YES;
  60. }
  61. #if !IS_MAS_BUILD()
  62. // This undocumented method notifies us if a user closes "Alert" notifications
  63. // https://chromium.googlesource.com/chromium/src/+/lkgr/chrome/browser/notifications/notification_platform_bridge_mac.mm
  64. - (void)userNotificationCenter:(NSUserNotificationCenter*)center
  65. didDismissAlert:(NSUserNotification*)notif {
  66. auto* notification = presenter_->GetNotification(notif);
  67. if (notification)
  68. notification->NotificationDismissed();
  69. }
  70. #endif
  71. #if !IS_MAS_BUILD()
  72. // This undocumented method notifies us if a user closes "Banner" notifications
  73. // https://github.com/mozilla/gecko-dev/blob/master/widget/cocoa/OSXNotificationCenter.mm
  74. - (void)userNotificationCenter:(NSUserNotificationCenter*)center
  75. didRemoveDeliveredNotifications:(NSArray*)notifications {
  76. for (NSUserNotification* notif in notifications) {
  77. auto* notification = presenter_->GetNotification(notif);
  78. if (notification)
  79. notification->NotificationDismissed();
  80. }
  81. }
  82. #endif
  83. @end