Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for rich notification #272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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).

Expand Down
77 changes: 70 additions & 7 deletions ios/RNCPushNotificationIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#import "RNCPushNotificationIOS.h"
#import "RCTConvert+Notification.h"

#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
Expand Down Expand Up @@ -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)
Expand Down