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

react-native-background-task is not working in background or foreground not in ios or android

Open rizwan92 opened this issue 6 years ago • 6 comments

hello author, we have been trying to using react-native-background-task as our native module in our app. so i started coding like this

import React, { Component } from 'react';
import { StyleSheet, Text, View} from 'react-native';
import BackgroundTask from 'react-native-background-task'
BackgroundTask.define(async () => {
       console.log('welcime to react native background task');
  BackgroundTask.finish()
})
type Props = {};
export default class App extends Component<Props> {
  constructor(props) {
    super(props);
  }

  state ={
    toggle:false,
  }

  componentDidMount() {
      this.getLocation();
  }
startBackgroundTask(){
  this.setState({toggle:!this.state.toggle},()=>{
      if (this.state.toggle) {
        BackgroundTask.schedule()
      }
    })
  }
}
  render() {
    return (
 <View style={styles.container}>
        <Text style={styles.welcome}>
           Start Your Back Ground Task
        </Text>
        <Switch value={this.state.toggle} disabled={false} onValueChange={this.startBackgroundTask.bind(this)}/>
   
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

it is not logging out in background or foreground

rizwan92 avatar May 11 '18 06:05 rizwan92

Same issue here. I'm waiting the desired time:

Keep in mind that after the app leaves the foreground, you'll have to wait at least 50% of the desired period (so, default is 7m30s) before the task executes.

But I can't see any task output. I am using log cat adb logcat *:S ReactNative:V ReactNativeJS:V BackgroundTask:V and writing to an external api.

Update: Just kidding. After wait about 8m the task was executed.

FSPinho avatar May 15 '18 21:05 FSPinho

It's not working for me either, this are my logs:

07-25 10:10:41.215 25542 27749 D BackgroundTask: Initializing
07-25 10:10:41.225 25542 25542 D BackgroundTask: Setting foreground true
07-25 10:10:42.638 25542 27748 I ReactNativeJS: Running application "RadioAppConcept" with appParams: {"rootTag":51}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
07-25 10:10:42.796 25542 27748 I ReactNativeJS: TEST
07-25 10:10:42.816 25542 27749 D BackgroundTask: @ReactMethod BackgroundTask.schedule

chriscoderdr avatar Jul 25 '18 14:07 chriscoderdr

Works on iOS if I press Debug-> Simulate background fetch. I've read you can't know when it's supposed to run...

jussirantala avatar Jul 31 '18 11:07 jussirantala

Works on iOS if I press Debug-> Simulate background fetch. I've read you can't know when it's supposed to run...

will it work when app is in foreground ?

0010abhi avatar Jan 10 '19 15:01 0010abhi

any solution yet?

HiralBarot1512 avatar Sep 16 '19 00:09 HiralBarot1512

Same here. It's not logging anything inside define block What to do?

artsnr1 avatar Jan 26 '21 17:01 artsnr1