Rapsssito / react-native-background-actions

React Native background service library for running background tasks forever in Android & iOS.
MIT License
818 stars 117 forks source link

I cant get location services when app is running in background but it works on emulators #120

Closed IsmailTan35 closed 2 years ago

IsmailTan35 commented 2 years ago
    const options = {
      taskName: 'Example',
      taskTitle: 'ExampleTask title',
      taskDesc: 'ExampleTask description',
      taskIcon: {
          name: 'ic_launcher',
          type: 'mipmap',
      },
      color: '#ff00ff',
      linkingURI: 'yourSchemeHere://chat/jane',
      parameters: {
          delay:6000,
      },
  };

  const connectSocket = () => {
    const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));
    const client = new WebSocket(`ws://${ip}:${port}`,'echo-protocol')

    const sendData = (data) => {
      if(client.readyState === client.OPEN){
        client.send(JSON.stringify(data))
      }
    }

    const coordianates = async(taskDataArguments) =>{
      const { delay } = taskDataArguments;
        await new Promise( async (resolve) => {
            for (let i = 0; BackgroundService.isRunning(); i++) {

                await BackgroundService.updateNotification({ taskDesc: 'Runned -> ' + i });
                Geolocation.getCurrentPosition(
                  (position) => {

                    sendData(position)

                  },
                  (error) => {
                    console.log(error.code, error.message);
                  },
                  { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
                );
                await sleep(delay);
              }
        });
    }

    client.onopen = async(err)=> {
      await BackgroundService.start(coordianates, options);
    }
  } 
Rapsssito commented 2 years ago

@IsmailTan35, I am sorry but for geolocation services I highly recommend you using react-native-geolocation instead. This module's scope is to be a general purpose background task manager for React Native, for audio or geolocation there are other specialized modules.