-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathNotifeeCore+UNUserNotificationCenter.m
More file actions
261 lines (223 loc) · 9.83 KB
/
NotifeeCore+UNUserNotificationCenter.m
File metadata and controls
261 lines (223 loc) · 9.83 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#import "NotifeeCore+UNUserNotificationCenter.h"
#import "NotifeeCoreDelegateHolder.h"
#import "NotifeeCoreUtil.h"
@implementation NotifeeCoreUNUserNotificationCenter
struct {
unsigned int willPresentNotification : 1;
unsigned int didReceiveNotificationResponse : 1;
unsigned int openSettingsForNotification : 1;
} originalUNCDelegateRespondsTo;
+ (instancetype)instance {
static dispatch_once_t once;
__strong static NotifeeCoreUNUserNotificationCenter *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[NotifeeCoreUNUserNotificationCenter alloc] init];
sharedInstance.initialNotification = nil;
sharedInstance.initialNotificationGathered = false;
sharedInstance.initialNotificationBlock = nil;
});
return sharedInstance;
}
- (void)observe {
static dispatch_once_t once;
__weak NotifeeCoreUNUserNotificationCenter *weakSelf = self;
dispatch_once(&once, ^{
NotifeeCoreUNUserNotificationCenter *strongSelf = weakSelf;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
if (center.delegate != nil) {
_originalDelegate = center.delegate;
originalUNCDelegateRespondsTo.openSettingsForNotification = (unsigned int)[_originalDelegate
respondsToSelector:@selector(userNotificationCenter:openSettingsForNotification:)];
originalUNCDelegateRespondsTo.willPresentNotification = (unsigned int)[_originalDelegate
respondsToSelector:@selector(userNotificationCenter:
willPresentNotification:withCompletionHandler:)];
originalUNCDelegateRespondsTo.didReceiveNotificationResponse =
(unsigned int)[_originalDelegate
respondsToSelector:@selector(userNotificationCenter:
didReceiveNotificationResponse:withCompletionHandler:)];
}
center.delegate = strongSelf;
});
}
- (void)onDidFinishLaunchingNotification:(nonnull NSDictionary *)notifUserInfo {
if (notifUserInfo != nil) {
NSDictionary *notifeeNotification = notifUserInfo[kNotifeeUserInfoNotification];
_initialNoticationID = notifeeNotification[@"id"];
}
_initialNotificationGathered = YES;
}
- (nullable NSDictionary *)getInitialNotification {
if (_initialNotificationGathered && _initialNotificationBlock != nil) {
// copying initial notification
if (_initialNotification != nil &&
[_initialNoticationID isEqualToString:_notificationOpenedAppID]) {
NSDictionary *initialNotificationCopy = [_initialNotification copy];
_initialNotification = nil;
_initialNotificationBlock(nil, initialNotificationCopy);
} else {
_initialNotificationBlock(nil, nil);
}
_initialNotificationBlock = nil;
}
return nil;
}
#pragma mark - UNUserNotificationCenter Delegate Methods
// The method will be called on the delegate only if the application is in the
// foreground. If the the handler is not called in a timely manner then the
// notification will not be presented. The application can choose to have the
// notification presented as a sound, badge, alert and/or in the notification
// list. This decision should be based on whether the information in the
// notification is otherwise visible to the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:
(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSDictionary *notifeeNotification =
notification.request.content.userInfo[kNotifeeUserInfoNotification];
if (notifeeNotification != nil) {
UNNotificationPresentationOptions presentationOptions = UNNotificationPresentationOptionNone;
NSDictionary *foregroundPresentationOptions =
notifeeNotification[@"ios"][@"foregroundPresentationOptions"];
BOOL alert = [foregroundPresentationOptions[@"alert"] boolValue];
BOOL badge = [foregroundPresentationOptions[@"badge"] boolValue];
BOOL sound = [foregroundPresentationOptions[@"sound"] boolValue];
BOOL banner = [foregroundPresentationOptions[@"banner"] boolValue];
BOOL list = [foregroundPresentationOptions[@"list"] boolValue];
if (badge) {
presentationOptions |= UNNotificationPresentationOptionBadge;
}
if (sound) {
presentationOptions |= UNNotificationPresentationOptionSound;
}
// if list or banner is true, ignore alert property
if (banner || list) {
if (banner) {
if (@available(iOS 14, *)) {
presentationOptions |= UNNotificationPresentationOptionBanner;
} else {
// for iOS 13 we need to set alert
presentationOptions |= UNNotificationPresentationOptionAlert;
}
}
if (list) {
if (@available(iOS 14, *)) {
presentationOptions |= UNNotificationPresentationOptionList;
} else {
// for iOS 13 we need to set alert
presentationOptions |= UNNotificationPresentationOptionAlert;
}
}
} else if (alert) {
// TODO: remove alert once it has been fully removed from the notifee API
presentationOptions |= UNNotificationPresentationOptionAlert;
}
// post DELIVERED event
[[NotifeeCoreDelegateHolder instance] didReceiveNotifeeCoreEvent:@{
@"type" : @(NotifeeCoreEventTypeDelivered),
@"detail" : @{
@"notification" : notifeeNotification,
}
}];
completionHandler(presentationOptions);
}
}
// The method will be called when the user responded to the notification by
// opening the application, dismissing the notification or choosing a
// UNNotificationAction. The delegate must be set before the application returns
// from application:didFinishLaunchingWithOptions:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler {
NSDictionary *notifeeNotification =
response.notification.request.content.userInfo[kNotifeeUserInfoNotification];
_notificationOpenedAppID = notifeeNotification[@"id"];
// handle notification outside of notifee
if (notifeeNotification == nil) {
if (_originalDelegate != nil && originalUNCDelegateRespondsTo.didReceiveNotificationResponse) {
[_originalDelegate userNotificationCenter:center
didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
} else {
notifeeNotification =
[NotifeeCoreUtil parseUNNotificationRequest:response.notification.request];
}
}
if (notifeeNotification != nil) {
if ([response.actionIdentifier isEqualToString:UNNotificationDismissActionIdentifier]) {
// post DISMISSED event, only triggers if notification has a categoryId
[[NotifeeCoreDelegateHolder instance] didReceiveNotifeeCoreEvent:@{
@"type" : @(NotifeeCoreEventTypeDismissed),
@"detail" : @{
@"notification" : notifeeNotification,
}
}];
return;
}
NSNumber *eventType;
NSMutableDictionary *event = [NSMutableDictionary dictionary];
NSMutableDictionary *eventDetail = [NSMutableDictionary dictionary];
NSMutableDictionary *eventDetailPressAction = [NSMutableDictionary dictionary];
if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
eventType = @1; // PRESS
// event.detail.pressAction.id
eventDetailPressAction[@"id"] = @"default";
} else {
eventType = @2; // ACTION_PRESS
// event.detail.pressAction.id
eventDetailPressAction[@"id"] = response.actionIdentifier;
}
if ([response isKindOfClass:UNTextInputNotificationResponse.class]) {
// event.detail.input
eventDetail[@"input"] = [(UNTextInputNotificationResponse *)response userText];
}
// event.type
event[@"type"] = eventType;
// event.detail.notification
eventDetail[@"notification"] = notifeeNotification;
// event.detail.pressAction
eventDetail[@"pressAction"] = eventDetailPressAction;
// event.detail
event[@"detail"] = eventDetail;
// store notification for getInitialNotification
_initialNotification = [eventDetail copy];
// post PRESS/ACTION_PRESS event
// Set is initial notification to true
if (_notificationOpenedAppID != nil &&
[_initialNoticationID isEqualToString:_notificationOpenedAppID]) {
eventDetail[@"initialNotification"] = @1;
}
[[NotifeeCoreDelegateHolder instance] didReceiveNotifeeCoreEvent:event];
// TODO figure out if this is needed or if we can just complete immediately
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
completionHandler();
});
}
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
openSettingsForNotification:(nullable UNNotification *)notification {
if (_originalDelegate != nil && originalUNCDelegateRespondsTo.openSettingsForNotification) {
if (@available(iOS 12.0, macOS 10.14, macCatalyst 13.0, *)) {
[_originalDelegate userNotificationCenter:center openSettingsForNotification:notification];
} else {
// Fallback on earlier versions
}
}
}
@end