Skip to content

Commit

Permalink
feat: add support for UNNotificationResponse in app 'ready' event (el…
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored Oct 28, 2020
1 parent bf89237 commit d2727f5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
5 changes: 3 additions & 2 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions docs/api/structures/notification-response.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion docs/tutorial/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,4 @@ Then, in your Electron application, require the module:
```js
const S3 = require('aws-sdk/clients/s3')
```
```
1 change: 1 addition & 0 deletions filenames.auto.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 39 additions & 7 deletions shell/browser/mac/electron_application_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +43,29 @@ - (void)_coreAttributesFromRange:(NSRange)range
@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 {
Expand Down Expand Up @@ -68,16 +93,23 @@ - (void)applicationWillFinishLaunching:(NSNotification*)notify {
}

- (void)applicationDidFinishLaunching:(NSNotification*)notify {
NSUserNotification* user_notification =
NSObject* user_notification =
[notify userInfo][NSApplicationLaunchUserNotificationKey];

if ([user_notification isKindOfClass:[NSUserNotification class]]) {
electron::Browser::Get()->DidFinishLaunching(
electron::NSDictionaryToDictionaryValue(user_notification.userInfo));
} else {
electron::Browser::Get()->DidFinishLaunching(base::DictionaryValue());
NSDictionary* notification_info = nil;

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()) {
Expand Down

0 comments on commit d2727f5

Please sign in to comment.