1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#import <EXNotifications/EXBackgroundNotificationTasksModule.h>
4#import <EXNotifications/EXBackgroundRemoteNotificationConsumer.h>
5#import <ExpoModulesCore/EXTaskManagerInterface.h>
6
7@interface EXBackgroundNotificationTasksModule ()
8
9@property (nonatomic, weak) id<EXTaskManagerInterface> taskManager;
10
11@end
12
13@implementation EXBackgroundNotificationTasksModule
14
15EX_EXPORT_MODULE(ExpoBackgroundNotificationTasksModule);
16
17# pragma mark - EXModuleRegistryConsumer
18
19- (void)setModuleRegistry:(EXModuleRegistry *)moduleRegistry
20{
21  _taskManager = [moduleRegistry getModuleImplementingProtocol:@protocol(EXTaskManagerInterface)];
22}
23# pragma mark - Exported methods
24
25EX_EXPORT_METHOD_AS(registerTaskAsync,
26                    registerTaskWithName:(nonnull NSString *)taskName
27                    resolve:(EXPromiseResolveBlock)resolve
28                    reject:(EXPromiseRejectBlock)reject)
29{
30  if (![_taskManager hasBackgroundModeEnabled:@"remote-notification"]) {
31    return reject(
32                  @"E_BACKGROUND_REMOTE_NOTIFICATIONS_DISABLED",
33                  @"Background remote notifications have not been configured. To enable it, add `remote-notification` to `UIBackgroundModes` in the application's Info.plist file.",
34                  nil
35                  );
36  }
37
38  @try {
39    [_taskManager registerTaskWithName:taskName
40                              consumer:EXBackgroundRemoteNotificationConsumer.class
41                               options:@{}];
42  }
43  @catch (NSException *e) {
44    return reject(e.name, e.reason, nil);
45  }
46  resolve(nil);
47}
48
49EX_EXPORT_METHOD_AS(unregisterTaskAsync,
50                    unregisterTaskWithName:(nonnull NSString *)taskName
51                    resolve:(EXPromiseResolveBlock)resolve
52                    reject:(EXPromiseRejectBlock)reject)
53{
54  @try {
55    [_taskManager unregisterTaskWithName:taskName consumerClass:[EXBackgroundRemoteNotificationConsumer class]];
56  } @catch (NSException *e) {
57    return reject(e.name, e.reason, nil);
58  }
59  resolve(nil);
60}
61
62@end
63