FIRMessagingExtensionHelper.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #import "FIRMessagingExtensionHelper.h"
  17. #import "FIRMMessageCode.h"
  18. #import "FIRMessagingLogger.h"
  19. static NSString *const kPayloadOptionsName = @"fcm_options";
  20. static NSString *const kPayloadOptionsImageURLName = @"image";
  21. @interface FIRMessagingExtensionHelper ()
  22. @property(nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
  23. @property(nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
  24. @end
  25. @implementation FIRMessagingExtensionHelper
  26. - (void)populateNotificationContent:(UNMutableNotificationContent *)content
  27. withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler {
  28. self.contentHandler = [contentHandler copy];
  29. self.bestAttemptContent = content;
  30. NSString *currentImageURL = content.userInfo[kPayloadOptionsName][kPayloadOptionsImageURLName];
  31. if (!currentImageURL) {
  32. [self deliverNotification];
  33. return;
  34. }
  35. #if TARGET_OS_IOS
  36. NSURL *attachmentURL = [NSURL URLWithString:currentImageURL];
  37. if (attachmentURL) {
  38. [self loadAttachmentForURL:attachmentURL
  39. completionHandler:^(UNNotificationAttachment *attachment) {
  40. self.bestAttemptContent.attachments = @[ attachment ];
  41. [self deliverNotification];
  42. }];
  43. } else {
  44. FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageInvalidURL,
  45. @"The Image URL provided is invalid %@.", currentImageURL);
  46. [self deliverNotification];
  47. }
  48. #else
  49. [self deliverNotification];
  50. #endif
  51. }
  52. #if TARGET_OS_IOS
  53. - (void)loadAttachmentForURL:(NSURL *)attachmentURL
  54. completionHandler:(void (^)(UNNotificationAttachment *))completionHandler {
  55. __block UNNotificationAttachment *attachment = nil;
  56. NSURLSession *session = [NSURLSession
  57. sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  58. [[session
  59. downloadTaskWithURL:attachmentURL
  60. completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
  61. if (error != nil) {
  62. FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageNotDownloaded,
  63. @"Failed to download image given URL %@, error: %@\n",
  64. attachmentURL, error);
  65. completionHandler(attachment);
  66. return;
  67. }
  68. NSFileManager *fileManager = [NSFileManager defaultManager];
  69. NSString *fileExtension =
  70. [NSString stringWithFormat:@".%@", [response.suggestedFilename pathExtension]];
  71. NSURL *localURL = [NSURL
  72. fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExtension]];
  73. [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
  74. if (error) {
  75. FIRMessagingLoggerError(
  76. kFIRMessagingServiceExtensionLocalFileNotCreated,
  77. @"Failed to move the image file to local location: %@, error: %@\n", localURL,
  78. error);
  79. completionHandler(attachment);
  80. return;
  81. }
  82. attachment = [UNNotificationAttachment attachmentWithIdentifier:@""
  83. URL:localURL
  84. options:nil
  85. error:&error];
  86. if (error) {
  87. FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageNotAttached,
  88. @"Failed to create attachment with URL %@, error: %@\n",
  89. localURL, error);
  90. completionHandler(attachment);
  91. return;
  92. }
  93. completionHandler(attachment);
  94. }] resume];
  95. }
  96. #endif
  97. - (void)deliverNotification {
  98. if (self.contentHandler) {
  99. self.contentHandler(self.bestAttemptContent);
  100. }
  101. }
  102. @end