Created
June 16, 2021 22:29
-
-
Save ajsmth/6b7994009269553961e554d24601b5de to your computer and use it in GitHub Desktop.
Expo BackgroundFetch Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { StyleSheet, Text, View, Button } from "react-native"; | |
import * as BackgroundFetch from "expo-background-fetch"; | |
import * as TaskManager from "expo-task-manager"; | |
const BACKGROUND_FETCH_TASK = "background-fetch"; | |
// 1. Define the task by providing a name and the function that should be executed | |
// Note: This needs to be called in the global scope (e.g outside of your React components) | |
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => { | |
const now = Date.now(); | |
console.log( | |
`Got background fetch call at date: ${new Date(now).toISOString()}` | |
); | |
// Be sure to return the successful result type! | |
return BackgroundFetch.Result.NewData; | |
}); | |
// 2. Register the task at some point in your app by providing the same name, and some configuration options for how the background fetch should behave | |
// Note: This does NOT need to be in the global scope and CAN be used in your React components! | |
async function registerBackgroundFetchAsync() { | |
return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, { | |
minimumInterval: 60 * 15, // 15 minutes - minimum possible is ~10-15 minutes | |
stopOnTerminate: false, // android only, | |
startOnBoot: true, // android only | |
}); | |
} | |
// 3. (Optional) Unregister tasks by specifying the task name | |
// This will cancel any future background fetch calls that match the given name | |
// Note: This does NOT need to be in the global scope and CAN be used in your React components! | |
async function unregisterBackgroundFetchAsync() { | |
return BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK); | |
} | |
export default function BackgroundFetchScreen() { | |
const [isRegistered, setIsRegistered] = React.useState<boolean>(false); | |
const [status, setStatus] = | |
React.useState<BackgroundFetch.Status | null>(null); | |
React.useEffect(() => { | |
checkStatusAsync(); | |
}, []); | |
const checkStatusAsync = async () => { | |
const status = await BackgroundFetch.getStatusAsync(); | |
const isRegistered = await TaskManager.isTaskRegisteredAsync( | |
BACKGROUND_FETCH_TASK | |
); | |
setStatus(status); | |
setIsRegistered(isRegistered); | |
}; | |
const toggleFetchTask = async () => { | |
if (isRegistered) { | |
await unregisterBackgroundFetchAsync(); | |
} else { | |
await registerBackgroundFetchAsync(); | |
} | |
checkStatusAsync(); | |
}; | |
return ( | |
<View style={styles.screen}> | |
<View style={styles.textContainer}> | |
<Text> | |
Background fetch status:{" "} | |
<Text style={styles.boldText}> | |
{status ? BackgroundFetch.Status[status] : null} | |
</Text> | |
</Text> | |
<Text> | |
Background fetch task name:{" "} | |
<Text style={styles.boldText}> | |
{isRegistered ? BACKGROUND_FETCH_TASK : "Not registered yet!"} | |
</Text> | |
</Text> | |
</View> | |
<View style={styles.textContainer}></View> | |
<Button | |
title={ | |
isRegistered | |
? "Unregister BackgroundFetch task" | |
: "Register BackgroundFetch task" | |
} | |
onPress={toggleFetchTask} | |
/> | |
</View> | |
); | |
} | |
const styles = StyleSheet.create({ | |
screen: { | |
flex: 1, | |
justifyContent: "center", | |
alignItems: "center", | |
}, | |
textContainer: { | |
margin: 10, | |
}, | |
boldText: { | |
fontWeight: "bold", | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment