transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.43k stars 191 forks source link

Can not Schedule Background Task in Background in IOS #489

Closed AngelRmrz closed 1 month ago

AngelRmrz commented 1 month ago

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."

christocracy commented 1 month ago

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

AngelRmrz commented 1 month ago

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?

christocracy commented 1 month ago

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>