diff options
| author | Pliable Pixels <pliablepixels@gmail.com> | 2019-02-25 10:32:31 -0500 |
|---|---|---|
| committer | Pliable Pixels <pliablepixels@gmail.com> | 2019-02-25 10:32:31 -0500 |
| commit | 7fe1c3505ca504668a3beff427498c5fd5a42dc7 (patch) | |
| tree | fbab1e18d790d9e118fd4283dbd4ac68788a400f /www/external/NotificationService.m | |
| parent | 69d03dea7de9cf4c2029139bb91dfc94c565fdba (diff) | |
added objective C remote notification code - simpler to manage for packaging
Diffstat (limited to 'www/external/NotificationService.m')
| -rw-r--r-- | www/external/NotificationService.m | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/www/external/NotificationService.m b/www/external/NotificationService.m new file mode 100644 index 00000000..143b92b0 --- /dev/null +++ b/www/external/NotificationService.m @@ -0,0 +1,93 @@ +// +// NotificationService.m +// NotificationService +// +// Created by User on 29/09/16. +// +// + +#import "NotificationService.h" + +@interface NotificationService () + +@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); +@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; + +@end + +@implementation NotificationService + + +- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { + self.contentHandler = contentHandler; + self.bestAttemptContent = [request.content mutableCopy]; + NSDictionary *userInfo = request.content.userInfo; + + // LP_URL is the key that is used from Leanplum to + // send the image URL in the payload. + // + // 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); + return; + } + + NSString *attachmentMedia = userInfo[@"image_url_jpg"]; + //NSLog (@"Your attachment URL is: %@", attachmentMedia); + + // 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]; + } + +} + +- (NSString*)determineType:(NSString *) fileType { + // Determines the file type of the attachment to append to NSURL. + + return @".jpg"; + + +} + +- (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. + self.contentHandler(self.bestAttemptContent); +} + +@end + |
