transistorsoft/react-native-background-fetch

Can not Schedule Background Task in Background in IOS

AngelRmrz opened this issue · 3 comments

  • Plugin version: react-native-background-fetch": "^4.2.2"
  • Platform: iOs
  • OS version: 17.0.1
  • Device manufacturer / model: Simulator / iPhone 12
  • React Native version (react-native -v): 0.72.6
  • Plugin config

On this case we use an .env but it is com.transistorsoft.fetch

	const handleStartDownload = () => {
		scheduleBackgroundAction();
	};

	const initBackgroundDownloadService = async () => {
		let responseStatus: number = await BackgroundFetch.configure(
			{
				minimumFetchInterval: 15,
				enableHeadless: true,
				forceAlarmManager: true,
				startOnBoot: true,
				stopOnTerminate: false,
				requiresCharging: false,
				requiresDeviceIdle: false,
				requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY,
			},
			async (taskId) => {
				console.log('[js] Received background-fetch event: ', taskId);
				BackgroundFetch.finish(taskId);
			},
			() => {
				console.log('[js] RNBackgroundFetch failed to start');
			},
		);
		setStatus(responseStatus);
		setEnabled(true);
	};

	const scheduleBackgroundAction = () => {
		console.info('[js] Scheduling task...');
		BackgroundFetch.scheduleTask({
			taskId: DOWNLOAD_TASK_ID,
			delay: 0,
			forceAlarmManager: true,
		}).then((taskId) => {
			console.log('[js] Task scheduled: ', taskId);
		});
	};

	React.useEffect(() => {
		initBackgroundDownloadService();
	}, []);

This is my info.plist config:

        <array>
		<string>com.transistorsoft.fetch</string>
	</array>

And this is my AppDelegate.mm config:

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import "RNSplashScreen.h"
#import "KtdraApp-Swift.h"
#import <TSBackgroundFetch/TSBackgroundFetch.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  self.moduleName = @"KtdraApp";
  self.initialProps = @{};

    
  BOOL success = [super application:application didFinishLaunchingWithOptions:launchOptions];
  if(success){
    //This is where we will put the logic to get access to rootview
    UIView *rootView = self.window.rootViewController.view;
    
    rootView.backgroundColor = [UIColor whiteColor];
 
    Dynamic *t = [Dynamic new];
    UIView *animationUIView = (UIView *)[t createAnimationViewWithRootView:rootView lottieName:@"splash_screen"]; // change lottieName to your lottie files name
 
    // register LottieSplashScreen to RNSplashScreen
    [RNSplashScreen showLottieSplash:animationUIView inRootView:rootView];
    // casting UIView type to AnimationView type
    LottieAnimationView *animationView = (LottieAnimationView *) animationUIView;
    // play
    [t playWithAnimationView:animationView];
    // If you want the animation layout to be forced to remove when hide is called, use this code
    [RNSplashScreen setAnimationFinished:true];
    [[TSBackgroundFetch sharedInstance] didFinishLaunching];
  }
  return success;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end

The expected behavior will be the output of the console.log() statements, but on every time i keep receiving this log: "No se pudo completar la operación. Background procssing task was not registered in AppDelegate didFinishLaunchingWithOptions. See iOS Setup Guide."

You need to register a dedicated taskId in your plist specifically for .scheduleTask

Can you be more specific @christocracy? I thought that its all referred to the first one, on this case will be the com.transistorsoft.fetch but if i understand correctly, this one is just to activate the background manager, i need one more per task or bunch of task?

This will not run on iOS.

BackgroundFetch.scheduleTask({
			taskId: DOWNLOAD_TASK_ID,
			delay: 0,
			forceAlarmManager: true,
		}).then((taskId) => {
			console.log('[js] Task scheduled: ', taskId);
		});
<array>
		<string>com.transistorsoft.fetch</string>
	</array>