Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a click handler for the "other" / close button #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
Expand Down
3 changes: 3 additions & 0 deletions notification_center_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
- (void)userNotificationCenter:(NSUserNotificationCenter *)center
didActivateNotification:(NSUserNotification *)notification;

- (void)userNotificationCenter:(NSUserNotificationCenter *)center
didDismissAlert:(NSUserNotification *)alert;

@end
24 changes: 21 additions & 3 deletions notification_center_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
bool isReply;
std::string response;
std::string id;
bool isOtherButton;
};

@implementation NotificationCenterDelegate
Expand All @@ -26,14 +27,15 @@ static void AsyncSendHandler(uv_async_t *handle) {

NSLog(@"Invoked notification with id: %s", info->id.c_str());

v8::Local<v8::Value> argv[3] = {
v8::Local<v8::Value> 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;
Expand All @@ -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.
*/
Expand All @@ -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;
Expand Down