From 4dd2d4ad57f52d9545804839fb327e24a12872ce Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Wed, 6 Nov 2019 18:43:09 +0000 Subject: [PATCH] Notifications are not shown on macOS caused by #3651 Fixes https://github.com/brave/brave-browser/issues/6786 --- .../browser/notification_helper_mac.h | 3 +- .../browser/notification_helper_mac.mm | 58 ++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/components/brave_ads/browser/notification_helper_mac.h b/components/brave_ads/browser/notification_helper_mac.h index 211bcd2f49b5..0d366a485723 100644 --- a/components/brave_ads/browser/notification_helper_mac.h +++ b/components/brave_ads/browser/notification_helper_mac.h @@ -22,7 +22,8 @@ class NotificationHelperMac NotificationHelperMac(); ~NotificationHelperMac() override; - bool IsNotificationsEnabled() const; + bool IsEnabled() const; + bool IsAuthorized() const; // NotificationHelper impl bool ShouldShowNotifications() override; diff --git a/components/brave_ads/browser/notification_helper_mac.mm b/components/brave_ads/browser/notification_helper_mac.mm index ae86dcfdf918..767257dc93a5 100644 --- a/components/brave_ads/browser/notification_helper_mac.mm +++ b/components/brave_ads/browser/notification_helper_mac.mm @@ -38,7 +38,12 @@ return true; } - if (!IsNotificationsEnabled()) { + if (!IsAuthorized()) { + LOG(INFO) << "Notification not made: User denied authorization"; + return false; + } + + if (!IsEnabled()) { LOG(INFO) << "Notification not made: Notifications are disabled"; return false; } @@ -64,7 +69,52 @@ /////////////////////////////////////////////////////////////////////////////// -bool NotificationHelperMac::IsNotificationsEnabled() const { +bool NotificationHelperMac::IsAuthorized() const { +#if !defined(OFFICIAL_BUILD) + LOG(WARNING) << "Unable to detect the status of native notifications on non" + " official builds as the app is not code signed"; + return true; +#else + // TODO(https://openradar.appspot.com/27768556): We must mock this function + // using NotificationHelperMock as a workaround to UNUserNotificationCenter + // throwing an exception during tests + + if (@available(macOS 10.14, *)) { + __block bool is_authorized = false; + + dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); + + UNUserNotificationCenter *notificationCenter = + [UNUserNotificationCenter currentNotificationCenter]; + + [notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert + completionHandler:^(BOOL granted, NSError * _Nullable error) { + if (granted) { + LOG(INFO) << "User granted authorization to show notifications"; + } else { + LOG(WARNING) << "User denied authorization to show notifications"; + } + + is_authorized = granted; + + dispatch_semaphore_signal(semaphore); + }]; + + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); + dispatch_release(semaphore); + + if (!is_authorized) { + LOG(WARNING) << "User is not authorized to show notifications"; + } + + return is_authorized; + } + + return true; +#endif +} + +bool NotificationHelperMac::IsEnabled() const { #if !defined(OFFICIAL_BUILD) LOG(WARNING) << "Unable to detect the status of native notifications on non" " official builds as the app is not code signed"; @@ -116,6 +166,10 @@ dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); dispatch_release(semaphore); + if (!is_authorized) { + LOG(WARNING) << "Notifications not authorized"; + } + return is_authorized; }