javascriptreactjsreact-nativebackground-process

React-native-background-action Cannot read property 'start' of null


i have a problem with repository react-native-background-action. It showing issue Type Error, cannot read property 'start' of null Picture is attached enter image description here

My code exemple

import { View, Button } from 'react-native';
import BackgroundService from 'react-native-background-actions';

const sleep = (time:number) => new Promise<void>((resolve) => setTimeout(() => resolve(), time));

const veryIntensiveTask = async (taskDataArguments:any) => {
    console.log("in intensive task");
    const { delay } = taskDataArguments;
    await new Promise( async (resolve) => {
        for (let i = 0; BackgroundService.isRunning(); i++) {
            console.log(i);
            await sleep(delay);
        }
    });
};

const options = {
    taskName: 'Example',
    taskTitle: 'ExampleTask title',
    taskDesc: 'ExampleTask description',
    taskIcon: {
        name: 'ic_launcher',
        type: 'mipmap',
    },
    color: '#ff00ff',
    parameters: {
        delay: 1000,
    },
};

export default function Background(){
const startBackgoundJob=async ()=>{
    await BackgroundService.start(veryIntensiveTask, options);
    console.log("background service started");
};

const updateBackgroundJob=async ()=>{
    await BackgroundService.updateNotification({taskDesc: 'New ExampleTask description'}); 
    console.log("background service updated");
};
const stopBackgroundJob=async ()=>{
    await BackgroundService.stop();
    console.log("background service stopped");
};
return(
    <View style={{flex:1,display:"flex",justifyContent:"center",alignItems:"center"}}>
        <Button title="start background job" onPress={startBackgoundJob}/>
        <Button title="update background job" onPress={updateBackgroundJob}/>
        <Button title="stop background job" onPress={stopBackgroundJob}/>
    </View>
)
}

The rn-foreground-service showing same issue.

Please let me know if you know how fix it.

Short about what i need. I need a function what will get the data like current time when the application is inactive or closed.

The code what i am past there, it is a just example.

If you know any repository can help me with it please let me know


Solution

    1. Try to delete the app and reinstall it. Since you probably updated the AndroidManifest.xlm including new permissions, it needs to be reinstalled, and its easy to forget that.
    2. If you already tried that, as mentioned in this PR from devtyty, when using Android 14, you need to change some files (probably this will be fixed soon by the mantainer of the library).

    Change your code like described in this image

    If you want to check the original PR, check it out here.