Skip to content

Commit

Permalink
Refactored notification service
Browse files Browse the repository at this point in the history
  • Loading branch information
erkanyildiz committed Sep 21, 2017
1 parent d82e9c5 commit f79a24a
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions CountlyNotificationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
NSString* const kCountlyPNKeyActionButtonTitle = @"t";
NSString* const kCountlyPNKeyActionButtonURL = @"l";

@interface CountlyNotificationService ()
#if TARGET_OS_IOS
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
#endif
@end

@implementation CountlyNotificationService
#if TARGET_OS_IOS
+ (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *))contentHandler
Expand All @@ -48,14 +41,13 @@ + (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withConte
return;
}

COUNTLY_EXT_LOG(@"notification modification in progress...");

COUNTLY_EXT_LOG(@"Checking for notification modifiers...");
UNMutableNotificationContent* bestAttemptContent = request.content.mutableCopy;

NSArray* buttons = countlyPayload[kCountlyPNKeyButtons];
if (buttons && buttons.count)
if (buttons.count)
{
COUNTLY_EXT_LOG(@"custom action buttons found: %d", (int)buttons.count);
COUNTLY_EXT_LOG(@"%d custom action buttons found.", (int)buttons.count);

NSMutableArray* actions = NSMutableArray.new;

Expand All @@ -73,29 +65,34 @@ + (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withConte
[UNUserNotificationCenter.currentNotificationCenter setNotificationCategories:[NSSet setWithObject:category]];

bestAttemptContent.categoryIdentifier = categoryIdentifier;

COUNTLY_EXT_LOG(@"%d custom action buttons added.", (int)buttons.count);
}

NSString* attachment = countlyPayload[kCountlyPNKeyAttachment];
if (attachment && attachment.length)
if (!attachment.length)
{
COUNTLY_EXT_LOG(@"attachment found: %@", attachment);
COUNTLY_EXT_LOG(@"No attachment specified in Countly payload.");
COUNTLY_EXT_LOG(@"Handling of notification finished.");
contentHandler(bestAttemptContent);
return;
}

COUNTLY_EXT_LOG(@"Attachment specified in Countly payload: %@", attachment);

[[NSURLSession.sharedSession downloadTaskWithURL:[NSURL URLWithString:attachment] completionHandler:^(NSURL * location, NSURLResponse * response, NSError * error)
[[NSURLSession.sharedSession downloadTaskWithURL:[NSURL URLWithString:attachment] completionHandler:^(NSURL * location, NSURLResponse * response, NSError * error)
{
if (!error)
{
if (error)
{
COUNTLY_EXT_LOG(@"attachment download error: %@", error);
}
else
{
COUNTLY_EXT_LOG(@"attachment download completed!");
COUNTLY_EXT_LOG(@"Attachment download completed!");

NSString* attachmentFileName = [NSString stringWithFormat:@"%@-%@", notificationID, response.suggestedFilename ?: response.URL.absoluteString.lastPathComponent];
NSString* attachmentFileName = [NSString stringWithFormat:@"%@-%@", notificationID, response.suggestedFilename ?: response.URL.absoluteString.lastPathComponent];

NSString* tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:attachmentFileName];
NSString* tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:attachmentFileName];

if (location && tempPath)
[NSFileManager.defaultManager moveItemAtPath:location.path toPath:tempPath error:&error];
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];
Expand All @@ -104,26 +101,26 @@ + (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withConte
{
bestAttemptContent.attachments = @[attachment];

COUNTLY_EXT_LOG(@"attachment added to notification!");
COUNTLY_EXT_LOG(@"Attachment added to notification!");
}
else
{
COUNTLY_EXT_LOG(@"attachment creation error: %@", attachmentError);
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);
}

contentHandler(bestAttemptContent);

COUNTLY_EXT_LOG(@"notification modification completed.");

}] resume];
}
else
{
COUNTLY_EXT_LOG(@"Handling of notification finished.");
contentHandler(bestAttemptContent);

COUNTLY_EXT_LOG(@"notification modification completed.");
}
}] resume];
}
#endif
@end

0 comments on commit f79a24a

Please sign in to comment.