1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#import <EXNotifications/EXNotificationPresentationModule.h>
4
5#import <EXNotifications/EXNotificationBuilder.h>
6
7@interface EXNotificationPresentationModule ()
8
9@property (nonatomic, weak) id<EXNotificationBuilder> notificationBuilder;
10
11@end
12
13@implementation EXNotificationPresentationModule
14
15UM_EXPORT_MODULE(ExpoNotificationPresenter);
16
17# pragma mark - Exported methods
18
19UM_EXPORT_METHOD_AS(presentNotificationAsync,
20                    presentNotificationWithIdentifier:(NSString *)identifier
21                    notification:(NSDictionary *)notificationSpec
22                    resolve:(UMPromiseResolveBlock)resolve
23                    reject:(UMPromiseRejectBlock)reject)
24{
25  UNNotificationContent *content = [_notificationBuilder notificationContentFromRequest:notificationSpec];
26  UNNotificationTrigger *trigger = nil;
27  UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
28  [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
29    if (error) {
30      NSString *message = [NSString stringWithFormat:@"Notification could not have been presented: %@", error.description];
31      reject(@"ERR_NOTIF_PRESENT", message, error);
32    } else {
33      resolve(nil);
34    }
35  }];
36}
37
38# pragma mark - UMModuleRegistryConsumer
39
40- (void)setModuleRegistry:(UMModuleRegistry *)moduleRegistry
41{
42  _notificationBuilder = [moduleRegistry getModuleImplementingProtocol:@protocol(EXNotificationBuilder)];
43}
44
45@end
46