transistorsoft / react-native-background-fetch

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

Why its not doing things in background? #448

Closed xts-bit closed 11 months ago

xts-bit commented 12 months ago

I am trying to upload a file to the cloud in the background so I am using this package but the problem is this is sometimes working and sometimes not i noticed most of the images when I close the app what i am doing wrong and how can i fix this problem with my code?

Also, Do react-native-background-fetch Can do any task if the app is closed but the device and internet are on (ON BOTH IOS AND ANDROID)


const ImageHandler = async () => {
    return new Promise(async (resolve, reject) => {
        try {
            // Uploading Image to Cloud
            if (data.message === 'done successfully') {
                resolve();
            } else {
                reject(new Error('Something went wrong, please try again'));
            }
        } catch (error) {
            reject(error);
        }
    });
};

export default function App() {

    useEffect(() => {
        BackgroundFetch.configure(
            {
                minimumFetchInterval: 15,
                stopOnTerminate: false,
                startOnBoot: true,
                enableHeadless: true,
                requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY
            },
            async (taskId) => {
                console.log('[BackgroundFetch HeadlessTask] start');
                await uploadHandler(taskId);
            },
            (error) => {
                console.log('[BackgroundFetch] ERROR: ', error);
            }
        );
    }, []);

    const uploadHandler = async (_taskId) => {
            try {
               // Upload File Function
            } catch (error) {
                console.log(error);
            }
        } else {
            alert('Please wait its processing');
        }
        BackgroundFetch.finish(taskId);
    };

    return (
       // JSX
    )
};