Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
toggle = async () => {
if (this.state.isRegistered) {
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
} else {
await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
minimumInterval: 60, // 1 minute
stopOnTerminate: false,
startOnBoot: true,
});
}
this.setState({ isRegistered: !this.state.isRegistered });
}
toggle = async () => {
if (this.state.isRegistered) {
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
} else {
await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
minimumInterval: 60, // 1 minute
stopOnTerminate: false,
startOnBoot: true,
});
}
this.setState({ isRegistered: !this.state.isRegistered });
}
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
const now = Date.now();
console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`);
await AsyncStorage.setItem(LAST_FETCH_DATE_KEY, now.toString());
return BackgroundFetch.Result.NewData;
});
render() {
return (
async checkStatusAsync() {
const status = await BackgroundFetch.getStatusAsync();
const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK);
this.setState({ status, isRegistered });
}