Skip to content

Commit

Permalink
Notifications are not shown on macOS caused by #3651
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey authored and mbacchi committed Nov 6, 2019
1 parent 0c9433d commit 4dd2d4a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
3 changes: 2 additions & 1 deletion components/brave_ads/browser/notification_helper_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class NotificationHelperMac
NotificationHelperMac();
~NotificationHelperMac() override;

bool IsNotificationsEnabled() const;
bool IsEnabled() const;
bool IsAuthorized() const;

// NotificationHelper impl
bool ShouldShowNotifications() override;
Expand Down
58 changes: 56 additions & 2 deletions components/brave_ads/browser/notification_helper_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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";
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 4dd2d4a

Please sign in to comment.