|
7 | 7 |
|
8 | 8 | #import "RNCPushNotificationIOS.h"
|
9 | 9 | #import "RCTConvert+Notification.h"
|
10 |
| - |
11 | 10 | #import <React/RCTBridge.h>
|
12 | 11 | #import <React/RCTConvert.h>
|
13 | 12 | #import <React/RCTEventDispatcher.h>
|
@@ -290,13 +289,77 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
|
290 | 289 | RCT_EXPORT_METHOD(addNotificationRequest:(UNNotificationRequest*)request)
|
291 | 290 | {
|
292 | 291 | UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
293 |
| - [center addNotificationRequest:request |
294 |
| - withCompletionHandler:^(NSError* _Nullable error) { |
295 |
| - if (!error) { |
296 |
| - NSLog(@"notifier request success"); |
| 292 | + NSString *imageUrl = request.content.userInfo[@"image"]; |
| 293 | + NSMutableDictionary *fcmInfo = request.content.userInfo[@"fcm_options"]; |
| 294 | + if(fcmInfo != nil && fcmInfo[@"image"] != nil) { |
| 295 | + imageUrl = fcmInfo[@"image"]; |
| 296 | + } |
| 297 | + if(imageUrl != nil) { |
| 298 | + NSURL *attachmentURL = [NSURL URLWithString:imageUrl]; |
| 299 | + [self loadAttachmentForUrl:attachmentURL completionHandler:^(UNNotificationAttachment *attachment) { |
| 300 | + if (attachment) { |
| 301 | + UNMutableNotificationContent *bestAttemptRequest = [request.content mutableCopy]; |
| 302 | + [bestAttemptRequest setAttachments: [NSArray arrayWithObject:attachment]]; |
| 303 | + UNNotificationRequest* notification = [UNNotificationRequest requestWithIdentifier:request.identifier content:bestAttemptRequest trigger:request.trigger]; |
| 304 | + [center addNotificationRequest:notification |
| 305 | + withCompletionHandler:^(NSError* _Nullable error) { |
| 306 | + if (!error) { |
| 307 | + NSLog(@"image notifier request success"); |
| 308 | + } |
| 309 | + } |
| 310 | + ]; |
297 | 311 | }
|
298 |
| - } |
299 |
| - ]; |
| 312 | + }]; |
| 313 | + } else { |
| 314 | + [center addNotificationRequest:request |
| 315 | + withCompletionHandler:^(NSError* _Nullable error) { |
| 316 | + if (!error) { |
| 317 | + NSLog(@"notifier request success"); |
| 318 | + } |
| 319 | + } |
| 320 | + ]; |
| 321 | + } |
| 322 | + |
| 323 | +} |
| 324 | + |
| 325 | +- (void)loadAttachmentForUrl:(NSURL *)attachmentURL |
| 326 | + completionHandler:(void (^)(UNNotificationAttachment *))completionHandler { |
| 327 | + __block UNNotificationAttachment *attachment = nil; |
| 328 | + |
| 329 | + NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; |
| 330 | + |
| 331 | + [[session |
| 332 | + downloadTaskWithURL:attachmentURL |
| 333 | + completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) { |
| 334 | + if (error != nil) { |
| 335 | + NSLog( @"Failed to download image given URL %@, error: %@\n", attachmentURL, error); |
| 336 | + completionHandler(attachment); |
| 337 | + return; |
| 338 | + } |
| 339 | + |
| 340 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 341 | + NSString *fileExtension = |
| 342 | + [NSString stringWithFormat:@".%@", [response.suggestedFilename pathExtension]]; |
| 343 | + NSURL *localURL = [NSURL |
| 344 | + fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExtension]]; |
| 345 | + [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error]; |
| 346 | + if (error) { |
| 347 | + NSLog( @"Failed to move the image file to local location: %@, error: %@\n", localURL, error); |
| 348 | + completionHandler(attachment); |
| 349 | + return; |
| 350 | + } |
| 351 | + |
| 352 | + attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" |
| 353 | + URL:localURL |
| 354 | + options:nil |
| 355 | + error:&error]; |
| 356 | + if (error) { |
| 357 | + NSLog(@"Failed to create attachment with URL %@, error: %@\n", localURL, error); |
| 358 | + completionHandler(attachment); |
| 359 | + return; |
| 360 | + } |
| 361 | + completionHandler(attachment); |
| 362 | + }] resume]; |
300 | 363 | }
|
301 | 364 |
|
302 | 365 | RCT_EXPORT_METHOD(setNotificationCategories:(NSArray*)categories)
|
|
0 commit comments