-
Notifications
You must be signed in to change notification settings - Fork 244
/
CountlyNotificationService.m
131 lines (102 loc) · 5.18 KB
/
CountlyNotificationService.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
// CountlyNotificationService.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyNotificationService.h"
#if DEBUG
#define COUNTLY_EXT_LOG(fmt, ...) NSLog([@"%@ " stringByAppendingString:fmt], @"[CountlyNSE]", ##__VA_ARGS__)
#else
#define COUNTLY_EXT_LOG(...)
#endif
NSString* const kCountlyActionIdentifier = @"CountlyActionIdentifier";
NSString* const kCountlyCategoryIdentifier = @"CountlyCategoryIdentifier";
NSString* const kCountlyPNKeyCountlyPayload = @"c";
NSString* const kCountlyPNKeyNotificationID = @"i";
NSString* const kCountlyPNKeyButtons = @"b";
NSString* const kCountlyPNKeyDefaultURL = @"l";
NSString* const kCountlyPNKeyAttachment = @"a";
NSString* const kCountlyPNKeyActionButtonIndex = @"b";
NSString* const kCountlyPNKeyActionButtonTitle = @"t";
NSString* const kCountlyPNKeyActionButtonURL = @"l";
@implementation CountlyNotificationService
#if (TARGET_OS_IOS || TARGET_OS_VISION)
+ (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *))contentHandler
{
COUNTLY_EXT_LOG(@"didReceiveNotificationRequest:withContentHandler:");
NSDictionary* countlyPayload = request.content.userInfo[kCountlyPNKeyCountlyPayload];
NSString* notificationID = countlyPayload[kCountlyPNKeyNotificationID];
if (!notificationID)
{
COUNTLY_EXT_LOG(@"Countly payload not found in notification dictionary!");
contentHandler(request.content);
return;
}
COUNTLY_EXT_LOG(@"Checking for notification modifiers...");
UNMutableNotificationContent* bestAttemptContent = request.content.mutableCopy;
NSArray* buttons = countlyPayload[kCountlyPNKeyButtons];
if (buttons.count)
{
COUNTLY_EXT_LOG(@"%d custom action buttons found.", (int)buttons.count);
NSMutableArray* actions = NSMutableArray.new;
[buttons enumerateObjectsUsingBlock:^(NSDictionary* button, NSUInteger idx, BOOL * stop)
{
NSString* actionIdentifier = [NSString stringWithFormat:@"%@%lu", kCountlyActionIdentifier, (unsigned long)idx + 1];
UNNotificationAction* action = [UNNotificationAction actionWithIdentifier:actionIdentifier title:button[kCountlyPNKeyActionButtonTitle] options:UNNotificationActionOptionForeground];
[actions addObject:action];
}];
NSString* categoryIdentifier = [kCountlyCategoryIdentifier stringByAppendingString:notificationID];
UNNotificationCategory* category = [UNNotificationCategory categoryWithIdentifier:categoryIdentifier actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
[UNUserNotificationCenter.currentNotificationCenter setNotificationCategories:[NSSet setWithObject:category]];
bestAttemptContent.categoryIdentifier = categoryIdentifier;
COUNTLY_EXT_LOG(@"%d custom action buttons added.", (int)buttons.count);
}
NSString* attachmentURL = countlyPayload[kCountlyPNKeyAttachment];
if (!attachmentURL.length)
{
COUNTLY_EXT_LOG(@"No attachment specified in Countly payload.");
COUNTLY_EXT_LOG(@"Handling of notification finished.");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
contentHandler(bestAttemptContent);
});
return;
}
COUNTLY_EXT_LOG(@"Attachment specified in Countly payload: %@", attachmentURL);
[[NSURLSession.sharedSession downloadTaskWithURL:[NSURL URLWithString:attachmentURL] completionHandler:^(NSURL * location, NSURLResponse * response, NSError * error)
{
if (!error)
{
COUNTLY_EXT_LOG(@"Attachment download completed!");
NSString* attachmentFileName = [NSString stringWithFormat:@"%@-%@", notificationID, response.suggestedFilename ?: response.URL.absoluteString.lastPathComponent];
NSString* tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:attachmentFileName];
if (location && tempPath)
{
[NSFileManager.defaultManager moveItemAtPath:location.path toPath:tempPath error:nil];
NSError* attachmentError = nil;
UNNotificationAttachment* attachment = [UNNotificationAttachment attachmentWithIdentifier:attachmentFileName URL:[NSURL fileURLWithPath:tempPath] options:nil error:&attachmentError];
if (attachment && !attachmentError)
{
bestAttemptContent.attachments = @[attachment];
COUNTLY_EXT_LOG(@"Attachment added to notification!");
}
else
{
COUNTLY_EXT_LOG(@"Attachment creation error: %@", attachmentError);
}
}
else
{
COUNTLY_EXT_LOG(@"Attachment `location` and/or `tempPath` is nil!");
}
}
else
{
COUNTLY_EXT_LOG(@"Attachment download error: %@", error);
}
COUNTLY_EXT_LOG(@"Handling of notification finished.");
contentHandler(bestAttemptContent);
}] resume];
}
#endif
@end