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