Skip to content

Commit 26e767e

Browse files
vuletuanbtvuletuan
andauthored
Support for rich notification (#272)
Co-authored-by: vuletuan <[email protected]>
1 parent 7fc8f16 commit 26e767e

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ export const App = () => {
234234
};
235235
};
236236
```
237+
## How to recieve rich notification in remote
238+
239+
Follow these [article](https://firebase.google.com/docs/cloud-messaging/ios/send-image) to create rich notification for your own
237240

238241
# Reference
239242

@@ -292,6 +295,7 @@ details is an object containing:
292295
- `isSilent` : If true, the notification will appear without sound (optional).
293296
- `category` : The category of this notification, required for actionable notifications (optional).
294297
- `userInfo` : An object containing additional notification data (optional).
298+
- `image` : It's useful if you need to diplay rich notification (optional).
295299
- `applicationIconBadgeNumber` The number to display as the app's icon badge. Setting the number to 0 removes the icon badge (optional).
296300
- `repeatInterval` : The interval to repeat as a string. Possible values: `minute`, `hour`, `day`, `week`, `month`, `year` (optional).
297301

ios/RNCPushNotificationIOS.m

+70-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#import "RNCPushNotificationIOS.h"
99
#import "RCTConvert+Notification.h"
10-
1110
#import <React/RCTBridge.h>
1211
#import <React/RCTConvert.h>
1312
#import <React/RCTEventDispatcher.h>
@@ -290,13 +289,77 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
290289
RCT_EXPORT_METHOD(addNotificationRequest:(UNNotificationRequest*)request)
291290
{
292291
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+
];
297311
}
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];
300363
}
301364

302365
RCT_EXPORT_METHOD(setNotificationCategories:(NSArray*)categories)

0 commit comments

Comments
 (0)