From a4e360f16cfed9fdde32fdbf3098cc80504f672f Mon Sep 17 00:00:00 2001 From: vuletuanbt Date: Wed, 17 Mar 2021 15:33:58 +0700 Subject: [PATCH 1/2] feat: support for rich notification --- ios/RNCPushNotificationIOS.m | 77 ++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/ios/RNCPushNotificationIOS.m b/ios/RNCPushNotificationIOS.m index 89adf61d9..18a5520a0 100644 --- a/ios/RNCPushNotificationIOS.m +++ b/ios/RNCPushNotificationIOS.m @@ -7,7 +7,6 @@ #import "RNCPushNotificationIOS.h" #import "RCTConvert+Notification.h" - #import #import #import @@ -284,13 +283,77 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification RCT_EXPORT_METHOD(addNotificationRequest:(UNNotificationRequest*)request) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; - [center addNotificationRequest:request - withCompletionHandler:^(NSError* _Nullable error) { - if (!error) { - NSLog(@"notifier request success"); + NSString *imageUrl = request.content.userInfo[@"image"]; + NSMutableDictionary *fcmInfo = request.content.userInfo[@"fcm_options"]; + if(fcmInfo != nil && fcmInfo[@"image"] != nil) { + imageUrl = fcmInfo[@"image"]; + } + if(imageUrl != nil) { + NSURL *attachmentURL = [NSURL URLWithString:imageUrl]; + [self loadAttachmentForUrl:attachmentURL completionHandler:^(UNNotificationAttachment *attachment) { + if (attachment) { + UNMutableNotificationContent *bestAttemptRequest = [request.content mutableCopy]; + [bestAttemptRequest setAttachments: [NSArray arrayWithObject:attachment]]; + UNNotificationRequest* notification = [UNNotificationRequest requestWithIdentifier:request.identifier content:bestAttemptRequest trigger:request.trigger]; + [center addNotificationRequest:notification + withCompletionHandler:^(NSError* _Nullable error) { + if (!error) { + NSLog(@"image notifier request success"); + } + } + ]; } - } - ]; + }]; + } else { + [center addNotificationRequest:request + withCompletionHandler:^(NSError* _Nullable error) { + if (!error) { + NSLog(@"notifier request success"); + } + } + ]; + } + +} + +- (void)loadAttachmentForUrl:(NSURL *)attachmentURL + completionHandler:(void (^)(UNNotificationAttachment *))completionHandler { + __block UNNotificationAttachment *attachment = nil; + + NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; + + [[session + downloadTaskWithURL:attachmentURL + completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) { + if (error != nil) { + NSLog( @"Failed to download image given URL %@, error: %@\n", attachmentURL, error); + completionHandler(attachment); + return; + } + + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSString *fileExtension = + [NSString stringWithFormat:@".%@", [response.suggestedFilename pathExtension]]; + NSURL *localURL = [NSURL + fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExtension]]; + [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error]; + if (error) { + NSLog( @"Failed to move the image file to local location: %@, error: %@\n", localURL, error); + completionHandler(attachment); + return; + } + + attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" + URL:localURL + options:nil + error:&error]; + if (error) { + NSLog(@"Failed to create attachment with URL %@, error: %@\n", localURL, error); + completionHandler(attachment); + return; + } + completionHandler(attachment); + }] resume]; } RCT_EXPORT_METHOD(setNotificationCategories:(NSArray*)categories) From c0ca3864e446d3b8a5f89f611616cc34de0e7df5 Mon Sep 17 00:00:00 2001 From: vuletuan Date: Wed, 17 Mar 2021 16:47:29 +0700 Subject: [PATCH 2/2] create docs --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 1ac090d68..10c53d695 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,9 @@ export const App = () => { }; }; ``` +## How to recieve rich notification in remote + +Follow these [article](https://firebase.google.com/docs/cloud-messaging/ios/send-image) to create rich notification for your own # Reference @@ -284,6 +287,7 @@ details is an object containing: - `isSilent` : If true, the notification will appear without sound (optional). - `category` : The category of this notification, required for actionable notifications (optional). - `userInfo` : An object containing additional notification data (optional). + - `image` : It's useful if you need to diplay rich notification (optional). - `applicationIconBadgeNumber` The number to display as the app's icon badge. Setting the number to 0 removes the icon badge (optional). - `repeatInterval` : The interval to repeat as a string. Possible values: `minute`, `hour`, `day`, `week`, `month`, `year` (optional).