jamesisaac / react-native-background-task

Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
MIT License
1.11k stars 110 forks source link

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

Open rizwan92 opened 6 years ago

rizwan92 commented 6 years ago

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

FSPinho commented 6 years ago

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.

chriscoderdr commented 6 years ago

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
jussirantala commented 6 years ago

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

0010abhi commented 5 years ago

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 ?

HiralBarot1512 commented 5 years ago

any solution yet?

artsnr1 commented 3 years ago

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