FIRMessagingExtensionHelper.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // The `userInfo` property isn't available on newer versions of tvOS.
  31. #if TARGET_OS_IOS || TARGET_OS_OSX
  32. NSString *currentImageURL = content.userInfo[kPayloadOptionsName][kPayloadOptionsImageURLName];
  33. if (!currentImageURL) {
  34. [self deliverNotification];
  35. return;
  36. }
  37. NSURL *attachmentURL = [NSURL URLWithString:currentImageURL];
  38. if (attachmentURL) {
  39. [self loadAttachmentForURL:attachmentURL
  40. completionHandler:^(UNNotificationAttachment *attachment) {
  41. self.bestAttemptContent.attachments = @[ attachment ];
  42. [self deliverNotification];
  43. }];
  44. } else {
  45. FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageInvalidURL,
  46. @"The Image URL provided is invalid %@.", currentImageURL);
  47. [self deliverNotification];
  48. }
  49. #else
  50. [self deliverNotification];
  51. #endif
  52. }
  53. #if TARGET_OS_IOS || TARGET_OS_OSX
  54. - (void)loadAttachmentForURL:(NSURL *)attachmentURL
  55. completionHandler:(void (^)(UNNotificationAttachment *))completionHandler {
  56. __block UNNotificationAttachment *attachment = nil;
  57. NSURLSession *session = [NSURLSession
  58. sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  59. [[session
  60. downloadTaskWithURL:attachmentURL
  61. completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
  62. if (error != nil) {
  63. FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageNotDownloaded,
  64. @"Failed to download image given URL %@, error: %@\n",
  65. attachmentURL, error);
  66. completionHandler(attachment);
  67. return;
  68. }
  69. NSFileManager *fileManager = [NSFileManager defaultManager];
  70. NSString *fileExtension =
  71. [NSString stringWithFormat:@".%@", [response.suggestedFilename pathExtension]];
  72. NSURL *localURL = [NSURL
  73. fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExtension]];
  74. [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
  75. if (error) {
  76. FIRMessagingLoggerError(
  77. kFIRMessagingServiceExtensionLocalFileNotCreated,
  78. @"Failed to move the image file to local location: %@, error: %@\n", localURL,
  79. error);
  80. completionHandler(attachment);
  81. return;
  82. }
  83. attachment = [UNNotificationAttachment attachmentWithIdentifier:@""
  84. URL:localURL
  85. options:nil
  86. error:&error];
  87. if (error) {
  88. FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageNotAttached,
  89. @"Failed to create attachment with URL %@, error: %@\n",
  90. localURL, error);
  91. completionHandler(attachment);
  92. return;
  93. }
  94. completionHandler(attachment);
  95. }] resume];
  96. }
  97. #endif
  98. - (void)deliverNotification {
  99. if (self.contentHandler) {
  100. self.contentHandler(self.bestAttemptContent);
  101. }
  102. }
  103. @end