From cefdaf346d21bd0f0b1de7f1e8b7524c00527aa7 Mon Sep 17 00:00:00 2001 From: Tim Su Date: Tue, 3 Mar 2020 11:04:58 -0800 Subject: [PATCH 1/2] Handle clicks on the 'other' / close button --- index.js | 4 +++- notification_center_delegate.h | 3 +++ notification_center_delegate.mm | 24 +++++++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 3edf552..3913736 100644 --- a/index.js +++ b/index.js @@ -18,12 +18,14 @@ module.exports = class Notification extends EventTarget { options.body = options.body || ''; options.canReply = !!options.canReply; - const activated = (isReply, response, id) => { + const activated = (isReply, response, id, isOtherButton) => { const notification = this.getNotificationById(id); if (!notification) return; if (isReply) { notification.dispatchEvent({ type: 'reply', response }); + } else if (isOtherButton) { + notification.dispatchEvent({ type: 'other' }); } else { notification.dispatchEvent({ type: 'click' }); } diff --git a/notification_center_delegate.h b/notification_center_delegate.h index 62d4799..8ab30c4 100644 --- a/notification_center_delegate.h +++ b/notification_center_delegate.h @@ -15,4 +15,7 @@ - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification; +- (void)userNotificationCenter:(NSUserNotificationCenter *)center + didDismissAlert:(NSUserNotification *)alert; + @end diff --git a/notification_center_delegate.mm b/notification_center_delegate.mm index d39b8f8..57c46d6 100644 --- a/notification_center_delegate.mm +++ b/notification_center_delegate.mm @@ -7,6 +7,7 @@ bool isReply; std::string response; std::string id; + bool isOtherButton; }; @implementation NotificationCenterDelegate @@ -26,14 +27,15 @@ static void AsyncSendHandler(uv_async_t *handle) { NSLog(@"Invoked notification with id: %s", info->id.c_str()); - v8::Local argv[3] = { + v8::Local argv[4] = { Nan::New(info->isReply), Nan::New(info->response).ToLocalChecked(), - Nan::New(info->id).ToLocalChecked() + Nan::New(info->id).ToLocalChecked(), + Nan::New(info->isOtherButton) }; Nan::AsyncResource async_resource("NodeMacNotifier:AsyncSendHandler"); - info->callback->Call(3, argv, &async_resource); + info->callback->Call(4, argv, &async_resource); delete info; info = nullptr; @@ -55,6 +57,21 @@ - (id)initWithActivationCallback:(Nan::Callback *)onActivation return self; } +- (void)userNotificationCenter:(NSUserNotificationCenter *)center + didDismissAlert:(NSUserNotification *)notification +{ + auto *info = new NotificationActivationInfo(); + info->isReply = false; + info->id = notification.identifier.UTF8String; + info->callback = OnActivation; + info->isOtherButton = true; + + auto *async = new uv_async_t(); + async->data = info; + uv_async_init(uv_default_loop(), async, (uv_async_cb)AsyncSendHandler); + uv_async_send(async); +} + /** * Occurs when the user activates a notification by clicking it or replying. */ @@ -65,6 +82,7 @@ - (void)userNotificationCenter:(NSUserNotificationCenter *)center info->isReply = notification.activationType == NSUserNotificationActivationTypeReplied; info->id = notification.identifier.UTF8String; info->callback = OnActivation; + info->isOtherButton = false; if (info->isReply) { info->response = notification.response.string.UTF8String; From c630defb25c54cf29ce3cccc5cac261c9c3dc0bf Mon Sep 17 00:00:00 2001 From: Tim Su Date: Tue, 3 Mar 2020 11:08:31 -0800 Subject: [PATCH 2/2] Add readme instructions --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index dbd6f9a..6a0c4df 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ Additional parameters to the notification. * #### `options.canReply` (bool) If true, this notification will have a reply action button, and can emit the `reply` event. Maps to `NSUserNotification.hasReplyButton`. +* #### `options.otherButtonTitle` (string) + Label for the close button on the notification. Listen for clicks on this button by adding an event listener for `other`. + * #### `options.bundleId` (string) Set this to override the `NSBundle.bundleIdentifier` used for the notification. This is a brute force way for your notifications to take on the appropriate app icon.