-
Notifications
You must be signed in to change notification settings - Fork 244
/
CountlyPushNotifications.m
371 lines (291 loc) · 13.1 KB
/
CountlyPushNotifications.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// CountlyPushNotifications.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
NSString* const kCountlyReservedEventPushAction = @"[CLY]_push_action";
NSString* const kCountlyTokenError = @"kCountlyTokenError";
NSString* const kCountlyPNKeyPlatform = @"p";
NSString* const kCountlyPNKeyiOS = @"i";
NSString* const kCountlyPNKeymacOS = @"m";
//NOTE: Push Notification Test Modes
CLYPushTestMode const CLYPushTestModeDevelopment = @"CLYPushTestModeDevelopment";
CLYPushTestMode const CLYPushTestModeTestFlightOrAdHoc = @"CLYPushTestModeTestFlightOrAdHoc";
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_OSX)
@interface CountlyPushNotifications () <UNUserNotificationCenterDelegate>
@property (nonatomic) NSString* token;
#else
@interface CountlyPushNotifications ()
#endif
@end
#if (TARGET_OS_IOS || TARGET_OS_VISION)
#define CLYApplication UIApplication
#elif (TARGET_OS_OSX)
#define CLYApplication NSApplication
#endif
@implementation CountlyPushNotifications
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
+ (instancetype)sharedInstance
{
if (!CountlyCommon.sharedInstance.hasStarted)
return nil;
static CountlyPushNotifications* s_sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
}
return self;
}
#pragma mark ---
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_OSX)
- (void)startPushNotifications
{
if (!self.isEnabledOnInitialConfig)
return;
if (!CountlyConsentManager.sharedInstance.consentForPushNotifications)
return;
UNUserNotificationCenter.currentNotificationCenter.delegate = self;
[self swizzlePushNotificationMethods];
[CLYApplication.sharedApplication registerForRemoteNotifications];
#if (TARGET_OS_OSX)
UNNotificationResponse* notificationResponse = self.launchNotification.userInfo[NSApplicationLaunchUserNotificationKey];
if (notificationResponse)
[self userNotificationCenter:UNUserNotificationCenter.currentNotificationCenter didReceiveNotificationResponse:notificationResponse withCompletionHandler:^{}];
#endif
}
- (void)stopPushNotifications
{
if (!self.isEnabledOnInitialConfig)
return;
if (UNUserNotificationCenter.currentNotificationCenter.delegate == self)
UNUserNotificationCenter.currentNotificationCenter.delegate = nil;
[CLYApplication.sharedApplication unregisterForRemoteNotifications];
}
- (void)swizzlePushNotificationMethods
{
static BOOL alreadySwizzled;
if (alreadySwizzled)
return;
alreadySwizzled = YES;
Class appDelegateClass = CLYApplication.sharedApplication.delegate.class;
NSArray* selectors =
@[
@"application:didRegisterForRemoteNotificationsWithDeviceToken:",
@"application:didFailToRegisterForRemoteNotificationsWithError:",
];
for (NSString* selectorString in selectors)
{
SEL originalSelector = NSSelectorFromString(selectorString);
Method originalMethod = class_getInstanceMethod(appDelegateClass, originalSelector);
if (originalMethod == NULL)
{
Method method = class_getInstanceMethod(self.class, originalSelector);
IMP imp = method_getImplementation(method);
const char* methodTypeEncoding = method_getTypeEncoding(method);
class_addMethod(appDelegateClass, originalSelector, imp, methodTypeEncoding);
originalMethod = class_getInstanceMethod(appDelegateClass, originalSelector);
}
SEL countlySelector = NSSelectorFromString([@"Countly_" stringByAppendingString:selectorString]);
Method countlyMethod = class_getInstanceMethod(appDelegateClass, countlySelector);
method_exchangeImplementations(originalMethod, countlyMethod);
}
}
- (void)askForNotificationPermissionWithOptions:(NSUInteger)options completionHandler:(void (^)(BOOL granted, NSError * error))completionHandler
{
if (!CountlyConsentManager.sharedInstance.consentForPushNotifications)
return;
if (options == 0)
options = UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert;
[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError* error)
{
if (completionHandler)
completionHandler(granted, error);
[self sendToken];
}];
}
- (void)sendToken
{
if (!CountlyConsentManager.sharedInstance.consentForPushNotifications)
return;
if (!self.token)
return;
if ([self.token isEqualToString:kCountlyTokenError])
{
[self clearToken];
return;
}
if (self.sendPushTokenAlways)
{
[CountlyConnectionManager.sharedInstance sendPushToken:self.token];
return;
}
BOOL hasNotificationPermissionBefore = [CountlyPersistency.sharedInstance retrieveNotificationPermission];
[UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings* settings)
{
BOOL hasProvisionalPermission = NO;
if (@available(iOS 12.0, *))
{
hasProvisionalPermission = settings.authorizationStatus == UNAuthorizationStatusProvisional;
}
if (settings.authorizationStatus == UNAuthorizationStatusAuthorized || hasProvisionalPermission)
{
[CountlyConnectionManager.sharedInstance sendPushToken:self.token];
[CountlyPersistency.sharedInstance storeNotificationPermission:YES];
}
else if (hasNotificationPermissionBefore)
{
[self clearToken];
[CountlyPersistency.sharedInstance storeNotificationPermission:NO];
}
}];
}
- (void)clearToken
{
[CountlyConnectionManager.sharedInstance sendPushToken:@""];
}
- (void)openURL:(NSString *)URLString
{
if (!URLString)
return;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
#if (TARGET_OS_IOS || TARGET_OS_VISION)
[UIApplication.sharedApplication openURL:[NSURL URLWithString:URLString] options:@{} completionHandler:nil];
#elif (TARGET_OS_OSX)
[NSWorkspace.sharedWorkspace openURL:[NSURL URLWithString:URLString]];
#endif
});
}
- (void)recordActionForNotification:(NSDictionary *)userInfo clickedButtonIndex:(NSInteger)buttonIndex
{
if (!CountlyConsentManager.sharedInstance.consentForPushNotifications)
return;
NSDictionary* countlyPayload = userInfo[kCountlyPNKeyCountlyPayload];
NSString* notificationID = countlyPayload[kCountlyPNKeyNotificationID];
[self recordActionEvent:notificationID buttonIndex:buttonIndex];
}
- (void)recordActionEvent:(NSString *)notificationID buttonIndex:(NSInteger)buttonIndex
{
if (!notificationID)
return;
NSString* platform = @"unknown";
#if (TARGET_OS_IOS || TARGET_OS_VISION)
platform = kCountlyPNKeyiOS;
#elif (TARGET_OS_OSX)
platform = kCountlyPNKeymacOS;
#endif
NSDictionary* segmentation =
@{
kCountlyPNKeyNotificationID: notificationID,
kCountlyPNKeyActionButtonIndex: @(buttonIndex),
kCountlyPNKeyPlatform: platform,
};
[Countly.sharedInstance recordReservedEvent:kCountlyReservedEventPushAction segmentation:segmentation];
}
#pragma mark ---
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0), macos(10.14))
{
CLY_LOG_D(@"userNotificationCenter:willPresentNotification:withCompletionHandler:");
CLY_LOG_D(@"%@", notification.request.content.userInfo.description);
if (!self.doNotShowAlertForNotifications)
{
NSDictionary* countlyPayload = notification.request.content.userInfo[kCountlyPNKeyCountlyPayload];
NSString* notificationID = countlyPayload[kCountlyPNKeyNotificationID];
if (notificationID)
{
UNNotificationPresentationOptions presentationOption = UNNotificationPresentationOptionNone;
if (@available(iOS 14.0, tvOS 14.0, macOS 11.0, watchOS 7.0, *))
{
presentationOption = UNNotificationPresentationOptionList | UNNotificationPresentationOptionBanner;
}
else
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
presentationOption = UNNotificationPresentationOptionAlert;
#pragma GCC diagnostic pop
}
completionHandler(presentationOption);
}
}
id<UNUserNotificationCenterDelegate> appDelegate = (id<UNUserNotificationCenterDelegate>)CLYApplication.sharedApplication.delegate;
if ([appDelegate respondsToSelector:@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:)])
[appDelegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
else
completionHandler(UNNotificationPresentationOptionNone);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0), macos(10.14))
{
CLY_LOG_D(@"userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:");
CLY_LOG_D(@"%@", response.notification.request.content.userInfo.description);
if (CountlyConsentManager.sharedInstance.consentForPushNotifications)
{
NSDictionary* countlyPayload = response.notification.request.content.userInfo[kCountlyPNKeyCountlyPayload];
NSString* notificationID = countlyPayload[kCountlyPNKeyNotificationID];
if (notificationID)
{
NSInteger buttonIndex = 0;
NSString* URL = nil;
CLY_LOG_D(@"Action Identifier: %@", response.actionIdentifier);
if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier])
{
URL = countlyPayload[kCountlyPNKeyDefaultURL];
}
else if ([response.actionIdentifier hasPrefix:kCountlyActionIdentifier])
{
buttonIndex = [[response.actionIdentifier stringByReplacingOccurrencesOfString:kCountlyActionIdentifier withString:@""] integerValue];
URL = countlyPayload[kCountlyPNKeyButtons][buttonIndex - 1][kCountlyPNKeyActionButtonURL];
}
[self recordActionEvent:notificationID buttonIndex:buttonIndex];
[self openURL:URL];
}
}
id<UNUserNotificationCenterDelegate> appDelegate = (id<UNUserNotificationCenterDelegate>)CLYApplication.sharedApplication.delegate;
if ([appDelegate respondsToSelector:@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)])
[appDelegate userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
else
completionHandler();
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification API_AVAILABLE(ios(12.0), macos(10.14))
{
if (@available(iOS 12.0, *))
{
id<UNUserNotificationCenterDelegate> appDelegate = (id<UNUserNotificationCenterDelegate>)CLYApplication.sharedApplication.delegate;
if ([appDelegate respondsToSelector:@selector(userNotificationCenter:openSettingsForNotification:)])
[appDelegate userNotificationCenter:center openSettingsForNotification:notification];
}
}
#pragma mark ---
- (void)application:(CLYApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{}
- (void)application:(CLYApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{}
#endif
@end
@implementation NSObject (CountlyPushNotifications)
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_OSX)
- (void)Countly_application:(CLYApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
CLY_LOG_D(@"App didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken);
const char* bytes = [deviceToken bytes];
NSMutableString *token = NSMutableString.new;
for (NSUInteger i = 0; i < deviceToken.length; i++)
[token appendFormat:@"%02hhx", bytes[i]];
CountlyPushNotifications.sharedInstance.token = token;
[CountlyPushNotifications.sharedInstance sendToken];
[self Countly_application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)Countly_application:(CLYApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
CLY_LOG_D(@"App didFailToRegisterForRemoteNotificationsWithError: %@", error);
CountlyPushNotifications.sharedInstance.token = kCountlyTokenError;
[CountlyPushNotifications.sharedInstance sendToken];
[self Countly_application:application didFailToRegisterForRemoteNotificationsWithError:error];
}
#endif
#endif
@end