FIRMessagingExtensionHelper.m 4.7 KB

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