forked from jdx/node-mac-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification_center_delegate.mm
103 lines (83 loc) · 2.71 KB
/
notification_center_delegate.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "notification_center_delegate.h"
#include <string>
struct NotificationActivationInfo {
Nan::Callback *callback;
bool isReply;
std::string response;
std::string id;
bool isOtherButton;
};
@implementation NotificationCenterDelegate
static void DeleteAsyncHandle(uv_handle_t *handle) {
delete (uv_async_t *)handle;
}
/**
* This handler runs in the V8 context as a result of `uv_async_send`. Here we
* retrieve our event information and invoke the saved callback.
*/
static void AsyncSendHandler(uv_async_t *handle) {
Nan::HandleScope scope;
auto *info = static_cast<NotificationActivationInfo *>(handle->data);
NSLog(@"Invoked notification with id: %s", info->id.c_str());
v8::Local<v8::Value> argv[4] = {
Nan::New(info->isReply),
Nan::New(info->response).ToLocalChecked(),
Nan::New(info->id).ToLocalChecked(),
Nan::New(info->isOtherButton)
};
Nan::AsyncResource async_resource("NodeMacNotifier:AsyncSendHandler");
info->callback->Call(4, argv, &async_resource);
delete info;
info = nullptr;
handle->data = nullptr;
uv_close((uv_handle_t *)handle, DeleteAsyncHandle);
}
/**
* We save off the JavaScript callback here and initialize the libuv event
* loop, which is needed in order to invoke the callback.
*/
- (id)initWithActivationCallback:(Nan::Callback *)onActivation
{
if (self = [super init]) {
OnActivation = 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.
*/
- (void)userNotificationCenter:(NSUserNotificationCenter *)center
didActivateNotification:(NSUserNotification *)notification
{
auto *info = new NotificationActivationInfo();
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;
}
auto *async = new uv_async_t();
async->data = info;
uv_async_init(uv_default_loop(), async, (uv_async_cb)AsyncSendHandler);
uv_async_send(async);
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification
{
return YES;
}
@end