diff options
| author | Pliable Pixels <pliablepixels@gmail.com> | 2020-03-14 09:25:29 -0400 |
|---|---|---|
| committer | Pliable Pixels <pliablepixels@gmail.com> | 2020-03-14 09:25:29 -0400 |
| commit | 58c7e55399591a6a487a9519624bf1e8ad6ca20f (patch) | |
| tree | 8f376ee677f9ac424ee7e9e26b2e4fa98081f9f0 /etc/NotificationService.m | |
| parent | e484e767ebfdb729e0f87d2dce1094ae6b7dae94 (diff) | |
Allow mp4 files in notification
Diffstat (limited to 'etc/NotificationService.m')
| -rw-r--r-- | etc/NotificationService.m | 114 |
1 files changed, 74 insertions, 40 deletions
diff --git a/etc/NotificationService.m b/etc/NotificationService.m index ef03a273..c30eaed9 100644 --- a/etc/NotificationService.m +++ b/etc/NotificationService.m @@ -28,63 +28,97 @@ // If there is no image in the payload than // the code will still show the push notification. if (userInfo == nil || userInfo[@"image_url_jpg"] == nil) { - self.contentHandler(self.bestAttemptContent); + NSLog(@"zmNinja Notification: Did not get a payload or image"); + [self contentComplete]; return; } - NSString *attachmentMedia = userInfo[@"image_url_jpg"]; - //NSLog (@"Your attachment URL is: %@", attachmentMedia); + NSString *mediaUrl = userInfo[@"image_url_jpg"]; + // if (mediaType == nil) { + // NSLog(@"zmNinja Notification: No media type specified, assuming .jpg"); + // mediaType = @".jpg"; + // } - // If there is an image in the payload, this part - // will handle the downloading and displaying of the image. - if (attachmentMedia) { - NSURL *URL = [NSURL URLWithString:attachmentMedia]; - NSURLSession *LPSession = [NSURLSession sessionWithConfiguration: - [NSURLSessionConfiguration defaultSessionConfiguration]]; - [[LPSession downloadTaskWithURL:URL completionHandler: ^(NSURL *temporaryLocation, NSURLResponse *response, NSError *error) { - if (error) { - NSLog(@"zmNinja Push: Error with downloading rich push: %@", - [error localizedDescription]); - self.contentHandler(self.bestAttemptContent); - return; - } - - NSString *fileType = [self determineType: [response MIMEType]]; - NSString *fileName = [[temporaryLocation.path lastPathComponent] stringByAppendingString:fileType]; - NSString *temporaryDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName]; - [[NSFileManager defaultManager] moveItemAtPath:temporaryLocation.path toPath:temporaryDirectory error:&error]; - - NSError *attachmentError = nil; - UNNotificationAttachment *attachment = - [UNNotificationAttachment attachmentWithIdentifier:@"" - URL:[NSURL fileURLWithPath:temporaryDirectory] - options:nil - error:&attachmentError]; - if (attachmentError != NULL) { - NSLog(@"zmNinja push: Error with the rich push attachment: %@", - [attachmentError localizedDescription]); - self.contentHandler(self.bestAttemptContent); - return; - } - self.bestAttemptContent.attachments = @[attachment]; - self.contentHandler(self.bestAttemptContent); - [[NSFileManager defaultManager] removeItemAtPath:temporaryDirectory error:&error]; - }] resume]; - } + // load the attachment + [self loadAttachmentForUrlString:mediaUrl + + completionHandler:^(UNNotificationAttachment *attachment) { + if (attachment) { + self.bestAttemptContent.attachments = [NSArray arrayWithObject:attachment]; + } + [self contentComplete]; + }]; } - (NSString*)determineType:(NSString *) fileType { // Determines the file type of the attachment to append to NSURL. - + //return @".gif"; + // Determines the file type of the attachment to append to NSURL. + NSLog (@"zmNinja Notification: determineType got filetype=%@",fileType); + if ([fileType isEqualToString:@"image/jpeg"]){ + NSLog (@"zmNinja Notification: returning JPG"); return @".jpg"; + } + if ([fileType isEqualToString:@"video/mp4"]){ + NSLog (@"zmNinja Notification: returning MP4"); + return @".mp4"; + } + + if ([fileType isEqualToString:@"image/gif"]) { + NSLog (@"zmNinja Notification: returning GIF"); + return @".gif"; + } + if ([fileType isEqualToString:@"image/png"]) { + NSLog (@"zmNinja Notification: returning PNG"); + return @".png"; + + } + NSLog (@"zmNinja Notification: unrecognized filetype, returning JPG"); + return @".jpg"; } +- (void)loadAttachmentForUrlString:(NSString *)urlString + completionHandler:(void(^)(UNNotificationAttachment *))completionHandler { + + __block UNNotificationAttachment *attachment = nil; + NSURL *attachmentURL = [NSURL URLWithString:urlString]; + + NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; + [[session downloadTaskWithURL:attachmentURL + completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) { + if (error != nil) { + + NSLog(@"unable to add attachment: %@", error.localizedDescription); + + } else { + NSString *fileType = [self determineType: [response MIMEType]]; + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileType]]; + [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error]; + + NSError *attachmentError = nil; + attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError]; + if (attachmentError) { + + NSLog(@"unable to add attchment: %@", attachmentError.localizedDescription); + + } + } + completionHandler(attachment); + }] resume]; +} + +- (void)contentComplete { + self.contentHandler(self.bestAttemptContent); +} + - (void)serviceExtensionTimeWillExpire { // Called just before the extension will be terminated by the system. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. + NSLog (@"zmNinja Notification: Time about to expire, handing off to best attempt"); self.contentHandler(self.bestAttemptContent); } |
