Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ribeiroevandro/c49fede9ecef309bc2bc84c1bcda8f65 to your computer and use it in GitHub Desktop.
Save ribeiroevandro/c49fede9ecef309bc2bc84c1bcda8f65 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from "react";
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
FlatList,
StatusBar,
} from "react-native";
import { Header, Colors } from "react-native/Libraries/NewAppScreen";
import BackgroundFetch from "react-native-background-fetch";
function App() {
const [events, setEvents] = useState([]);
function addEvent(taskId) {
// Simulate a possibly long-running asynchronous task with a Promise.
return new Promise((resolve, reject) => {
setEvents([...events, { taskId, timestamp: new Date().toString() }]);
resolve();
});
}
async function initBackgroundFetch() {
// BackgroundFetch event handler.
const onEvent = async (taskId) => {
console.log("[BackgroundFetch] task: ", taskId);
// Do your background work...
await addEvent(taskId);
// IMPORTANT: You must signal to the OS that your task is complete.
BackgroundFetch.finish(taskId);
};
// Timeout callback is executed when your Task has exceeded its allowed running-time.
// You must stop what you're doing immediately BackgroundFetch.finish(taskId)
const onTimeout = async (taskId) => {
console.warn("[BackgroundFetch] TIMEOUT task: ", taskId);
BackgroundFetch.finish(taskId);
};
// Initialize BackgroundFetch only once when component mounts.
let status = await BackgroundFetch.configure(
{ minimumFetchInterval: 15 },
onEvent,
onTimeout
);
console.log("[BackgroundFetch] configure status: ", status);
}
useEffect(() => {
initBackgroundFetch();
}, []);
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}
>
<Header />
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>BackgroundFetch Demo</Text>
</View>
</View>
</ScrollView>
<View style={styles.sectionContainer}>
<FlatList
data={events}
renderItem={({ item }) => (
<Text>
[{item.taskId}]: {item.timestamp}
</Text>
)}
keyExtractor={(item) => item.timestamp}
/>
</View>
</SafeAreaView>
</>
);
}
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: "600",
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: "400",
color: Colors.dark,
},
});
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment