Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BackgroundService.start is create multiple task #121

Open
shinnachot opened this issue Jan 24, 2022 · 9 comments
Open

BackgroundService.start is create multiple task #121

shinnachot opened this issue Jan 24, 2022 · 9 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@shinnachot
Copy link

shinnachot commented Jan 24, 2022

I using this code when i want to start some task BackgroundService.start(this.taskRandom, options);
When i kill app or logout i use this code to stop task BackgroundService.stop();
It's work fine but...when i try to start task again BackgroundService.start(this.taskRandom, options); it'll do a multiple task job
Example
start task 1 13:05PM
stop
start task 1 13:10PM
start task 1 13:15PM
<<< it will double do job like this forever

Here is my code

class Home extends Component {

    playing = BackgroundService.isRunning();

    trackBackground = async () => {
        this.playing = !this.playing;
        if (this.playing) {
            try {
                console.log('Trying to start background service');
                await BackgroundService.start(this.taskRandom, options);
                console.log('Successful start!');
            } catch (e) {
                console.log('Error', e);
            }
        } else {
            console.log('Stop background service');
            await BackgroundService.stop();
        }
    };


    taskRandom = async (taskData) => {
        if (Platform.OS === 'ios') {
            console.warn(
                'This task will not keep your app alive in the background by itself, use other library like react-native-track-player that use audio,',
                'geolocalization, etc. to keep your app alive in the background while you excute the JS from this library.'
            );
        }
        await new Promise(async (resolve) => {
            // For loop with a delay
            const { delay } = taskData;
            for (let i = 0; BackgroundService.isRunning(); i++) {
                console.log('Send data');

                
                await BackgroundService.updateNotification({ taskDesc: 'Tracking...' + i });
                await sleep(delay);
            }
        });
    };

    componentWillUnmount() {
        BackgroundService.stop();
    }

    componentDidMount() {
        this.trackBackground();
    }
}
@Rapsssito
Copy link
Owner

@shinnachot, I am not sure, but you may need to resolve your promise:

await new Promise(async (resolve) => {
            // For loop with a delay
            const { delay } = taskData;
            for (let i = 0; BackgroundService.isRunning(); i++) {
                console.log('Send data');

                
                await BackgroundService.updateNotification({ taskDesc: 'Tracking...' + i });
                await sleep(delay);
            }
            resolve();
        });

@Rapsssito Rapsssito added the bug Something isn't working label Feb 22, 2022
@fabiros
Copy link
Contributor

fabiros commented Mar 3, 2022

+1
In my case the action start is called after a successful login. The action runs forever in the background and gets restarted during login with adjusted service values.
The action always runs the amount of times the app was killed simultaneously.
Personally I think the error has something todo with react native Appregistry.runApplication always creating a new RootTag

@fabiros
Copy link
Contributor

fabiros commented Mar 10, 2022

@Rapsssito i modified the pkg code locally and when i adjust the onStartCommand like this it works.

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final Bundle extras = intent.getExtras();
        if (extras == null) {
            throw new IllegalArgumentException("Extras cannot be null");
        }
        final Options bgOptions = new Options(extras);
        createNotificationChannel(bgOptions.getTaskTitle(), bgOptions.getTaskDesc()); // Necessary creating channel for API 26+
        // Create the notification
        final Notification notification = buildNotification(this, bgOptions);

        startForeground(SERVICE_NOTIFICATION_ID, notification);

        HeadlessJsTaskConfig taskConfig = this.getTaskConfig(intent);

        if (taskConfig != null) {
            this.stopForeground(false);
            this.startTask(taskConfig);
        }

        return START_NOT_STICKY;
    }

Dont know if this solves the problem, but worked for me

@Rapsssito
Copy link
Owner

@fabiros, could you create a PR with your code so we can have this "fix" in a branch at least?

@fabiros
Copy link
Contributor

fabiros commented Mar 13, 2022

@Rapsssito done

@Rapsssito
Copy link
Owner

This issue should be fixed by #124. Feel free to reopen if the problem persists.

@Rapsssito
Copy link
Owner

I have reopened the issue since I had to revert the PR (#124) because it was creating another issue: #126.

@shafnashah95
Copy link

Does any one found the solution for this?

@Rapsssito Rapsssito added help wanted Extra attention is needed and removed work-in-progress labels Jul 9, 2024
@a-eid
Copy link

a-eid commented Dec 31, 2024

we're facing this issue as well, we're solving it with an MMKV flag for now,

edit:

this did not solve the issue for me. it looks like .stop() doesn't stop the running services for some reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants