mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
* refactor: replace deprecated NSUserNotifications with User Notifications
Removes deprecated NSUserNotification API, now using User Notifications
It replaces API calls for generating, scheduling, and receiving native
macOS notifications with equivalent API calls from the new framework,
or functionally equivalent implementations.
To preserve the existing Notification module API, special handling was
required in certain cases:
- Dynamically declared notification actions
Typically, notification actions should be declared at app launch time
when using the User Notifications framework. However, this isn’t
compatible with Electron’s architecture. Instead, we dynamically
declare new notifications actions when necessary and carefully manage
the existing actions registered at runtime.
- Localizations for ‘Reply’ and ‘Show’ labels
New translation files are added and processed through GRIT to add
localizations for “Reply” and “Show” button labels which were
initially supplied by the NSUserNotification framework.
* Use NotificationImageRetainer pattern from //chrome
* build: fix lint
* build: update config to handle --translate-gender for pak files
* test: also sign on arm64
* fix: add error handling for scheduling notification
* docs: add details to breaking changes
* docs: clarify breaking change details
* docs: add details for notifications tutorial and API documentation
---------
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
84 lines
3.1 KiB
Plaintext
84 lines
3.1 KiB
Plaintext
// Copyright (c) 2015 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "base/logging.h"
|
|
#include "base/task/thread_pool.h"
|
|
|
|
#include "shell/browser/notifications/mac/notification_presenter_mac.h"
|
|
|
|
#include "shell/browser/notifications/mac/cocoa_notification.h"
|
|
#include "shell/browser/notifications/mac/notification_center_delegate.h"
|
|
|
|
namespace electron {
|
|
|
|
// static
|
|
std::unique_ptr<NotificationPresenter> NotificationPresenter::Create() {
|
|
return std::make_unique<NotificationPresenterMac>();
|
|
}
|
|
|
|
CocoaNotification* NotificationPresenterMac::GetNotification(
|
|
UNNotificationRequest* un_notification_request) {
|
|
for (Notification* notification : notifications()) {
|
|
auto* native_notification = static_cast<CocoaNotification*>(notification);
|
|
if ([native_notification->notification_request().identifier
|
|
isEqual:un_notification_request.identifier])
|
|
return native_notification;
|
|
}
|
|
|
|
if (electron::debug_notifications) {
|
|
LOG(INFO) << "Could not find notification for "
|
|
<< [un_notification_request.identifier UTF8String];
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
NotificationPresenterMac::NotificationPresenterMac()
|
|
: notification_center_delegate_(
|
|
[[NotificationCenterDelegate alloc] initWithPresenter:this]),
|
|
image_retainer_(std::make_unique<NotificationImageRetainer>()),
|
|
image_task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
|
|
{base::MayBlock(), base::TaskPriority::USER_VISIBLE})) {
|
|
// Delete any remaining temp files in the image folder from the previous
|
|
// sessions.
|
|
DCHECK(image_task_runner_);
|
|
auto cleanup_task = image_retainer_->GetCleanupTask();
|
|
image_task_runner_->PostTask(FROM_HERE, std::move(cleanup_task));
|
|
|
|
UNUserNotificationCenter* center =
|
|
[UNUserNotificationCenter currentNotificationCenter];
|
|
|
|
center.delegate = notification_center_delegate_;
|
|
|
|
[center
|
|
requestAuthorizationWithOptions:(UNAuthorizationOptionAlert |
|
|
UNAuthorizationOptionSound |
|
|
UNAuthorizationOptionBadge)
|
|
completionHandler:^(BOOL granted,
|
|
NSError* _Nullable error) {
|
|
if (electron::debug_notifications) {
|
|
if (error) {
|
|
LOG(INFO)
|
|
<< "Error requesting notification authorization: "
|
|
<< [error.localizedDescription UTF8String];
|
|
} else {
|
|
LOG(INFO) << "Notification authorization granted: "
|
|
<< granted;
|
|
}
|
|
}
|
|
}];
|
|
}
|
|
|
|
NotificationPresenterMac::~NotificationPresenterMac() {
|
|
UNUserNotificationCenter.currentNotificationCenter.delegate = nil;
|
|
image_task_runner_->DeleteSoon(FROM_HERE, image_retainer_.release());
|
|
}
|
|
|
|
Notification* NotificationPresenterMac::CreateNotificationObject(
|
|
NotificationDelegate* delegate) {
|
|
return new CocoaNotification(delegate, this);
|
|
}
|
|
|
|
} // namespace electron
|