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