react-native-background-actions icon indicating copy to clipboard operation
react-native-background-actions copied to clipboard

BackgroundService.start is create multiple task

Open shinnachot opened this issue 3 years ago • 8 comments

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();
    }
}

shinnachot avatar Jan 24 '22 16:01 shinnachot

@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 avatar Feb 22 '22 09:02 Rapsssito

+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 avatar Mar 03 '22 12:03 fabiros

@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

fabiros avatar Mar 10 '22 14:03 fabiros

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

Rapsssito avatar Mar 12 '22 09:03 Rapsssito

@Rapsssito done

fabiros avatar Mar 13 '22 19:03 fabiros

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

Rapsssito avatar Mar 22 '22 18:03 Rapsssito

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

Rapsssito avatar Apr 25 '22 14:04 Rapsssito

any update for create multiple task issue ?

oursintegrated avatar Jun 21 '22 03:06 oursintegrated