feat: add support for UNNotificationResponse in app 'ready' event (#25950)
This commit is contained in:
parent
bf89237f60
commit
d2727f5aba
5 changed files with 50 additions and 9 deletions
|
@ -33,10 +33,11 @@ In most cases, you should do everything in the `ready` event handler.
|
|||
Returns:
|
||||
|
||||
* `event` Event
|
||||
* `launchInfo` Record<string, any> _macOS_
|
||||
* `launchInfo` Record<string, any> | [NotificationResponse](structures/notification-response.md) _macOS_
|
||||
|
||||
Emitted once, when Electron has finished initializing. On macOS, `launchInfo`
|
||||
holds the `userInfo` of the `NSUserNotification` that was used to open the
|
||||
holds the `userInfo` of the `NSUserNotification` or information from
|
||||
[`UNNotificationResponse`](structures/notification-response.md) that was used to open the
|
||||
application, if it was launched from Notification Center. You can also call
|
||||
`app.isReady()` to check if this event has already fired and `app.whenReady()`
|
||||
to get a Promise that is fulfilled when Electron is initialized.
|
||||
|
|
7
docs/api/structures/notification-response.md
Normal file
7
docs/api/structures/notification-response.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
# NotificationResponse Object
|
||||
|
||||
* `actionIdentifier` String - The identifier string of the action that the user selected.
|
||||
* `date` Number - The delivery date of the notification.
|
||||
* `identifier` String - The unique identifier for this notification request.
|
||||
* `userInfo` Record<String, any> - A dictionary of custom information associated with the notification.
|
||||
* `userText` String (optional) - The text entered or chosen by the user.
|
|
@ -103,6 +103,7 @@ auto_filenames = {
|
|||
"docs/api/structures/mouse-wheel-input-event.md",
|
||||
"docs/api/structures/new-window-web-contents-event.md",
|
||||
"docs/api/structures/notification-action.md",
|
||||
"docs/api/structures/notification-response.md",
|
||||
"docs/api/structures/point.md",
|
||||
"docs/api/structures/post-body.md",
|
||||
"docs/api/structures/post-data.md",
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
#include "shell/browser/mac/dict_util.h"
|
||||
#import "shell/browser/mac/electron_application.h"
|
||||
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
|
||||
#if BUILDFLAG(USE_ALLOCATOR_SHIM)
|
||||
// On macOS 10.12, the IME system attempts to allocate a 2^64 size buffer,
|
||||
// which would typically cause an OOM crash. To avoid this, the problematic
|
||||
|
@ -41,6 +43,29 @@ static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session;
|
|||
@end
|
||||
#endif // BUILDFLAG(USE_ALLOCATOR_SHIM)
|
||||
|
||||
static NSDictionary* UNNotificationResponseToNSDictionary(
|
||||
UNNotificationResponse* response) API_AVAILABLE(macosx(10.14)) {
|
||||
// [response isKindOfClass:[UNNotificationResponse class]]
|
||||
if (![response respondsToSelector:@selector(actionIdentifier)] ||
|
||||
![response respondsToSelector:@selector(notification)]) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
|
||||
result[@"actionIdentifier"] = response.actionIdentifier;
|
||||
result[@"date"] = @(response.notification.date.timeIntervalSince1970);
|
||||
result[@"identifier"] = response.notification.request.identifier;
|
||||
result[@"userInfo"] = response.notification.request.content.userInfo;
|
||||
|
||||
// [response isKindOfClass:[UNTextInputNotificationResponse class]]
|
||||
if ([response respondsToSelector:@selector(userText)]) {
|
||||
result[@"userText"] =
|
||||
static_cast<UNTextInputNotificationResponse*>(response).userText;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@implementation ElectronApplicationDelegate
|
||||
|
||||
- (void)setApplicationDockMenu:(electron::ElectronMenuModel*)model {
|
||||
|
@ -68,16 +93,23 @@ static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session;
|
|||
}
|
||||
|
||||
- (void)applicationDidFinishLaunching:(NSNotification*)notify {
|
||||
NSUserNotification* user_notification =
|
||||
NSObject* user_notification =
|
||||
[notify userInfo][NSApplicationLaunchUserNotificationKey];
|
||||
NSDictionary* notification_info = nil;
|
||||
|
||||
if ([user_notification isKindOfClass:[NSUserNotification class]]) {
|
||||
electron::Browser::Get()->DidFinishLaunching(
|
||||
electron::NSDictionaryToDictionaryValue(user_notification.userInfo));
|
||||
} else {
|
||||
electron::Browser::Get()->DidFinishLaunching(base::DictionaryValue());
|
||||
if (user_notification) {
|
||||
if ([user_notification isKindOfClass:[NSUserNotification class]]) {
|
||||
notification_info =
|
||||
[static_cast<NSUserNotification*>(user_notification) userInfo];
|
||||
} else if (@available(macOS 10.14, *)) {
|
||||
notification_info = UNNotificationResponseToNSDictionary(
|
||||
static_cast<UNNotificationResponse*>(user_notification));
|
||||
}
|
||||
}
|
||||
|
||||
electron::Browser::Get()->DidFinishLaunching(
|
||||
electron::NSDictionaryToDictionaryValue(notification_info));
|
||||
|
||||
#if BUILDFLAG(USE_ALLOCATOR_SHIM)
|
||||
// Disable fatal OOM to hack around an OS bug https://crbug.com/654695.
|
||||
if (base::mac::IsOS10_12()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue