expo: Error: Task 'background-fetch' not found for app ID 'mainApplication'.
Summary
Error: Task ‘background-fetch’ not found for app ID ‘mainApplication’. When I add your Demo to my App
Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!
bare
What platform(s) does this occur on?
iOS
SDK Version (managed workflow only)
No response
Environment
expo-env-info 1.0.2 environment info: System: OS: macOS 12.3.1 Shell: 5.8 - /bin/zsh Binaries: Node: 16.14.2 - ~/Documents/Arbeit/VMZ/sensorcollector/node_modules/.bin/node npm: 8.5.5 - ~/Documents/Arbeit/VMZ/sensorcollector/node_modules/.bin/npm Watchman: 2022.03.21.00 - /usr/local/bin/watchman Managers: CocoaPods: 1.11.3 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.4, iOS 15.4, macOS 12.3, tvOS 15.4, watchOS 8.5 IDEs: Android Studio: 2021.1 AI-211.7628.21.2111.8309675 Xcode: 13.3/13E113 - /usr/bin/xcodebuild npmPackages: expo: ^44.0.6 => 44.0.6 react: ^17.0.2 => 17.0.2 react-native: 0.67.4 => 0.67.4 npmGlobalPackages: expo-cli: 5.3.0 Expo Workflow: bare
Reproducible demo
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.BackgroundFetchResult.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);
}
// 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(false);
React.useEffect(() => {
checkStatusAsync();
}, []);
const checkStatusAsync = async () => {
const status = await BackgroundFetch.getStatusAsync();
const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK);
setIsRegistered(isRegistered);
};
const toggleFetchTask = async () => {
if (isRegistered) {
await unregisterBackgroundFetchAsync();
} else {
await registerBackgroundFetchAsync();
}
checkStatusAsync();
};
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 6
- Comments: 21 (1 by maintainers)
Ok, so for anyone else getting this error, this seems to happen when you try to call either register or unregister on a task that doesn’t exist or hasn’t been defined.
For instance, I tried to to
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_TASK.CLEAR_CLIPBOARD);before I registered the task which resulted in the error.