diff options
| -rw-r--r-- | docs/remote extension.txt | 9 | ||||
| -rw-r--r-- | www/external/NotificationService.m | 93 |
2 files changed, 94 insertions, 8 deletions
diff --git a/docs/remote extension.txt b/docs/remote extension.txt index 1c45d41f..9fed3bcc 100644 --- a/docs/remote extension.txt +++ b/docs/remote extension.txt @@ -7,7 +7,7 @@ 531 open platforms/ios/ Open up platforms/ios/zmNinja.xcworkspace -File->New->Target->Notification Service Extension +File->New->Target->Notification Service Extension, select Objective C In the "Product Name" put in zmNinjaNotification (your BundleID should now read com.pliablepixels.zmninja-pro.zmNinjaNotification) Say "Yes" to "Activate zmNinjaNotification scheme?" popup Now in XCode Targets, select zmNinjaNotification, and make sure you select a Team and make sure Deployment Target is 10 or above @@ -19,13 +19,6 @@ Now in XCode Targets, select zmNinjaNotification, and make sure you select a Tea 535 pod install -Search path "${PROJECT_DIR}" recursive in zmNinjaNotification and archive? - -For Distribution: - -- always embed swift standard libraries to No in both? (every time after cordova prepare) -make versions same in both - only needed for product archive - - Manually change remote notification extension version to match app version 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 + |
