-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathnotification.m
More file actions
161 lines (142 loc) · 6.39 KB
/
Copy pathnotification.m
File metadata and controls
161 lines (142 loc) · 6.39 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#import <Cocoa/Cocoa.h>
#import <UserNotifications/UserNotifications.h>
#import "notification.h"
static NSMutableDictionary<NSString *, UNNotificationCategory *> *registeredCategories;
void initNotifications(void) {
registeredCategories = [NSMutableDictionary new];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert |
UNAuthorizationOptionSound |
UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError *error) {
if (error) {
NSLog(@"Notification authorization error: %@", error);
}
}];
}
static NSString *categoryIdentifierForActions(NSString *actionButton,
NSString *closeButton,
NSString *responsePlaceholder) {
return [NSString stringWithFormat:@"menuet_a:%@_c:%@_r:%@",
actionButton ?: @"",
closeButton ?: @"",
responsePlaceholder ?: @""];
}
static void ensureCategoryRegistered(NSString *actionButton,
NSString *closeButton,
NSString *responsePlaceholder) {
NSString *categoryId = categoryIdentifierForActions(actionButton,
closeButton,
responsePlaceholder);
@synchronized(registeredCategories) {
if (registeredCategories[categoryId]) {
return;
}
NSMutableArray<UNNotificationAction *> *actions = [NSMutableArray new];
if (responsePlaceholder.length > 0) {
UNTextInputNotificationAction *replyAction =
[UNTextInputNotificationAction actionWithIdentifier:@"menuet.reply"
title:@"Reply"
options:UNNotificationActionOptionNone
textInputButtonTitle:@"Send"
textInputPlaceholder:responsePlaceholder];
[actions addObject:replyAction];
}
if (actionButton.length > 0) {
UNNotificationAction *action =
[UNNotificationAction actionWithIdentifier:@"menuet.action"
title:actionButton
options:UNNotificationActionOptionForeground];
[actions addObject:action];
}
UNNotificationCategoryOptions categoryOptions = UNNotificationCategoryOptionNone;
if (closeButton.length > 0) {
categoryOptions |= UNNotificationCategoryOptionCustomDismissAction;
}
UNNotificationCategory *category =
[UNNotificationCategory categoryWithIdentifier:categoryId
actions:actions
intentIdentifiers:@[]
options:categoryOptions];
registeredCategories[categoryId] = category;
NSSet *categorySet = [NSSet setWithArray:registeredCategories.allValues];
[[UNUserNotificationCenter currentNotificationCenter]
setNotificationCategories:categorySet];
}
}
void showNotification(const char *jsonString) {
NSDictionary *jsonDict = [NSJSONSerialization
JSONObjectWithData:[[NSString stringWithUTF8String:jsonString]
dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:nil];
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = jsonDict[@"Title"] ?: @"";
content.subtitle = jsonDict[@"Subtitle"] ?: @"";
content.body = jsonDict[@"Message"] ?: @"";
if (![jsonDict[@"Silent"] boolValue]) {
content.sound = [UNNotificationSound defaultSound];
}
NSString *actionButton = jsonDict[@"ActionButton"];
NSString *closeButton = jsonDict[@"CloseButton"];
NSString *responsePlaceholder = jsonDict[@"ResponsePlaceholder"];
BOOL needsCategory = (actionButton.length > 0 ||
closeButton.length > 0 ||
responsePlaceholder.length > 0);
if (needsCategory) {
ensureCategoryRegistered(actionButton, closeButton, responsePlaceholder);
content.categoryIdentifier = categoryIdentifierForActions(actionButton,
closeButton,
responsePlaceholder);
}
NSString *identifier = jsonDict[@"Identifier"];
if (identifier.length == 0) {
identifier = [[NSUUID UUID] UUIDString];
}
BOOL removeFromNotificationCenter = [jsonDict[@"RemoveFromNotificationCenter"] boolValue];
UNNotificationRequest *request =
[UNNotificationRequest requestWithIdentifier:identifier
content:content
trigger:nil];
dispatch_async(dispatch_get_main_queue(), ^{
UNUserNotificationCenter *center =
[UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request
withCompletionHandler:^(NSError *error) {
if (error) {
NSLog(@"Notification error: %@", error);
}
if (removeFromNotificationCenter) {
[center removeDeliveredNotificationsWithIdentifiers:@[identifier]];
}
}];
});
}
// notificationAuthorizationStatus reports the app's current notification
// authorization as a static string: "authorized", "provisional", "denied",
// "notDetermined", or "unknown". The UN API is async-only; a semaphore makes
// it synchronous, which is safe because callers are Go goroutines, never the
// main thread (the completion handler runs on a UN internal queue, so waiting
// here cannot deadlock the main queue).
const char *notificationAuthorizationStatus(void) {
__block UNAuthorizationStatus status = -1;
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[[UNUserNotificationCenter currentNotificationCenter]
getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
status = settings.authorizationStatus;
dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC));
switch (status) {
case UNAuthorizationStatusAuthorized:
return "authorized";
case UNAuthorizationStatusProvisional:
return "provisional";
case UNAuthorizationStatusDenied:
return "denied";
case UNAuthorizationStatusNotDetermined:
return "notDetermined";
default:
return "unknown";
}
}