|
| 1 | +/* |
| 2 | + * Copyright 2019 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + #import "FIRMessagingExtensionHelper.h" |
| 18 | + |
| 19 | + #import "FIRMMessageCode.h" |
| 20 | +#import "FIRMessagingLogger.h" |
| 21 | + |
| 22 | + static NSString *const kPayloadOptionsName = @"fcm_options"; |
| 23 | +static NSString *const kPayloadOptionsImageURLName = @"image"; |
| 24 | + |
| 25 | + @interface FIRMessagingExtensionHelper () |
| 26 | +@property(nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); |
| 27 | +@property(nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; |
| 28 | + |
| 29 | + @end |
| 30 | + |
| 31 | + @implementation FIRMessagingExtensionHelper |
| 32 | + |
| 33 | + - (void)populateNotificationContent:(UNMutableNotificationContent *)content |
| 34 | + withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler { |
| 35 | + self.contentHandler = [contentHandler copy]; |
| 36 | + self.bestAttemptContent = content; |
| 37 | + |
| 38 | + NSString *currentImageURL = content.userInfo[kPayloadOptionsName][kPayloadOptionsImageURLName]; |
| 39 | + if (!currentImageURL) { |
| 40 | + [self deliverNotification]; |
| 41 | + return; |
| 42 | + } |
| 43 | +#if TARGET_OS_IOS |
| 44 | + NSURL *attachmentURL = [NSURL URLWithString:currentImageURL]; |
| 45 | + if (attachmentURL) { |
| 46 | + [self loadAttachmentForURL:attachmentURL |
| 47 | + completionHandler:^(UNNotificationAttachment *attachment) { |
| 48 | + self.bestAttemptContent.attachments = @[ attachment ]; |
| 49 | + [self deliverNotification]; |
| 50 | + }]; |
| 51 | + } else { |
| 52 | + FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageInvalidURL, |
| 53 | + @"The Image URL provided is invalid %@.", currentImageURL); |
| 54 | + [self deliverNotification]; |
| 55 | + } |
| 56 | +#else |
| 57 | + [self deliverNotification]; |
| 58 | +#endif |
| 59 | +} |
| 60 | + |
| 61 | + #if TARGET_OS_IOS |
| 62 | +- (void)loadAttachmentForURL:(NSURL *)attachmentURL |
| 63 | + completionHandler:(void (^)(UNNotificationAttachment *))completionHandler { |
| 64 | + __block UNNotificationAttachment *attachment = nil; |
| 65 | + |
| 66 | + NSURLSession *session = [NSURLSession |
| 67 | + sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; |
| 68 | + [[session |
| 69 | + downloadTaskWithURL:attachmentURL |
| 70 | + completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) { |
| 71 | + if (error != nil) { |
| 72 | + FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageNotDownloaded, |
| 73 | + @"Failed to download image given URL %@, error: %@\n", |
| 74 | + attachmentURL, error); |
| 75 | + completionHandler(attachment); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 80 | + NSString *fileExtension = |
| 81 | + [NSString stringWithFormat:@".%@", [response.suggestedFilename pathExtension]]; |
| 82 | + NSURL *localURL = [NSURL |
| 83 | + fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExtension]]; |
| 84 | + [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error]; |
| 85 | + if (error) { |
| 86 | + FIRMessagingLoggerError( |
| 87 | + kFIRMessagingServiceExtensionLocalFileNotCreated, |
| 88 | + @"Failed to move the image file to local location: %@, error: %@\n", localURL, |
| 89 | + error); |
| 90 | + completionHandler(attachment); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" |
| 95 | + URL:localURL |
| 96 | + options:nil |
| 97 | + error:&error]; |
| 98 | + if (error) { |
| 99 | + FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageNotAttached, |
| 100 | + @"Failed to create attachment with URL %@, error: %@\n", |
| 101 | + localURL, error); |
| 102 | + completionHandler(attachment); |
| 103 | + return; |
| 104 | + } |
| 105 | + completionHandler(attachment); |
| 106 | + }] resume]; |
| 107 | +} |
| 108 | +#endif |
| 109 | + |
| 110 | + - (void)deliverNotification { |
| 111 | + if (self.contentHandler) { |
| 112 | + self.contentHandler(self.bestAttemptContent); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | + @end |
| 117 | + |
0 commit comments