This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
forked from caseymrm/menuet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.m
48 lines (46 loc) · 1.78 KB
/
notification.m
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
#import <Cocoa/Cocoa.h>
#import "notification.h"
void showNotification(const char *jsonString) {
NSDictionary *jsonDict = [NSJSONSerialization
JSONObjectWithData:[[NSString stringWithUTF8String:jsonString]
dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:nil];
NSUserNotification *notification = [NSUserNotification new];
BOOL showsButtons = NO;
notification.title = jsonDict[@"Title"];
notification.subtitle = jsonDict[@"Subtitle"];
notification.informativeText = jsonDict[@"Message"];
NSString *identifier = jsonDict[@"Identifier"];
if (identifier.length > 0) {
notification.identifier = identifier;
}
NSString *closeButton = jsonDict[@"CloseButton"];
if (closeButton.length > 0) {
showsButtons = true;
notification.otherButtonTitle = closeButton;
}
NSString *actionButton = jsonDict[@"ActionButton"];
if (actionButton.length > 0) {
showsButtons = true;
notification.actionButtonTitle = actionButton;
}
NSString *responsePlaceholder = jsonDict[@"ResponsePlaceholder"];
if (responsePlaceholder.length > 0) {
notification.hasReplyButton = YES;
notification.responsePlaceholder = responsePlaceholder;
}
if (showsButtons) {
// Override banner setting, could check plist to see if we're already set to alerts
[notification setValue:@YES forKey:@"_showsButtons"];
}
BOOL removeFromNotificationCenter = [jsonDict[@"RemoveFromNotificationCenter"] boolValue];
dispatch_async(dispatch_get_main_queue(), ^{
NSUserNotificationCenter *center =
[NSUserNotificationCenter defaultUserNotificationCenter];
[center deliverNotification:notification];
if (removeFromNotificationCenter) {
[center removeDeliveredNotification:notification];
}
});
}