1// Copyright 2018-present 650 Industries. All rights reserved. 2 3#import <ABI47_0_0EXBackgroundFetch/ABI47_0_0EXBackgroundFetch.h> 4#import <ABI47_0_0EXBackgroundFetch/ABI47_0_0EXBackgroundFetchTaskConsumer.h> 5#import <ABI47_0_0ExpoModulesCore/ABI47_0_0EXTaskInterface.h> 6 7@implementation ABI47_0_0EXBackgroundFetchTaskConsumer 8 9+ (BOOL)supportsLaunchReason:(ABI47_0_0EXTaskLaunchReason)launchReason 10{ 11 return launchReason == ABI47_0_0EXTaskLaunchReasonBackgroundFetch; 12} 13 14- (NSString *)taskType 15{ 16 return @"backgroundFetch"; 17} 18 19// Associating task to the consumer. 20- (void)didRegisterTask:(id<ABI47_0_0EXTaskInterface>)task 21{ 22 _task = task; 23 [self updateMinimumInterval]; 24} 25 26- (void)setOptions:(NSDictionary *)options 27{ 28 [self updateMinimumInterval]; 29} 30 31// Method that is being called when the JS app just finished launching, 32// after the native app was launched with the launch reason supported by the consumer. 33// For background fetch, `application:performFetchWithCompletionHandler:` is the entry point of this method, 34// so the task can be executed immediately here. 35- (void)didBecomeReadyToExecuteWithData:(NSDictionary *)data 36{ 37 [_task executeWithData:nil withError:nil]; 38} 39 40// Translate result received from JS to another (native) type that is then used for example as an argument in completion callbacks. 41- (NSUInteger)normalizeTaskResult:(id)result 42{ 43 if (!result || result == [NSNull null]) { 44 return UIBackgroundFetchResultNoData; 45 } 46 switch ([result unsignedIntegerValue]) { 47 case ABI47_0_0EXBackgroundFetchResultNewData: 48 return UIBackgroundFetchResultNewData; 49 case ABI47_0_0EXBackgroundFetchResultFailed: 50 return UIBackgroundFetchResultFailed; 51 case ABI47_0_0EXBackgroundFetchResultNoData: 52 default: 53 return UIBackgroundFetchResultNoData; 54 } 55} 56 57- (void)updateMinimumInterval 58{ 59 NSNumber *interval = _task.options[@"minimumInterval"]; 60 NSTimeInterval timeInterval = [interval doubleValue] ?: UIApplicationBackgroundFetchIntervalMinimum; 61 62 dispatch_async(dispatch_get_main_queue(), ^{ 63 [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:timeInterval]; 64 }); 65} 66 67@end 68