1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#import <ABI47_0_0EXNotifications/ABI47_0_0EXBackgroundNotificationTasksModule.h>
4#import <ABI47_0_0EXNotifications/ABI47_0_0EXBackgroundRemoteNotificationConsumer.h>
5#import <ABI47_0_0ExpoModulesCore/ABI47_0_0EXTaskManagerInterface.h>
6
7@interface ABI47_0_0EXBackgroundNotificationTasksModule ()
8
9@property (nonatomic, weak) id<ABI47_0_0EXTaskManagerInterface> taskManager;
10
11@end
12
13@implementation ABI47_0_0EXBackgroundNotificationTasksModule
14
15ABI47_0_0EX_EXPORT_MODULE(ExpoBackgroundNotificationTasksModule);
16
17# pragma mark - ABI47_0_0EXModuleRegistryConsumer
18
19- (void)setModuleRegistry:(ABI47_0_0EXModuleRegistry *)moduleRegistry
20{
21  _taskManager = [moduleRegistry getModuleImplementingProtocol:@protocol(ABI47_0_0EXTaskManagerInterface)];
22}
23# pragma mark - Exported methods
24
25ABI47_0_0EX_EXPORT_METHOD_AS(registerTaskAsync,
26                    registerTaskWithName:(nonnull NSString *)taskName
27                    resolve:(ABI47_0_0EXPromiseResolveBlock)resolve
28                    reject:(ABI47_0_0EXPromiseRejectBlock)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:ABI47_0_0EXBackgroundRemoteNotificationConsumer.class
41                               options:@{}];
42  }
43  @catch (NSException *e) {
44    return reject(e.name, e.reason, nil);
45  }
46  resolve(nil);
47}
48
49ABI47_0_0EX_EXPORT_METHOD_AS(unregisterTaskAsync,
50                    unregisterTaskWithName:(nonnull NSString *)taskName
51                    resolve:(ABI47_0_0EXPromiseResolveBlock)resolve
52                    reject:(ABI47_0_0EXPromiseRejectBlock)reject)
53{
54  @try {
55    [_taskManager unregisterTaskWithName:taskName consumerClass:[ABI47_0_0EXBackgroundRemoteNotificationConsumer class]];
56  } @catch (NSException *e) {
57    return reject(e.name, e.reason, nil);
58  }
59  resolve(nil);
60}
61
62@end
63