react-native-background-task
react-native-background-task copied to clipboard
react-native-background-task is not working in background or foreground not in ios or android
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
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.
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
Works on iOS if I press Debug-> Simulate background fetch. I've read you can't know when it's supposed to run...
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 ?
any solution yet?
Same here. It's not logging anything inside define block What to do?