I’m coding in react native and I want to make my app run in the background on android phones only. I want to allow my phone to open a webbrowser in the background in react native without the app being open. How can I do this?
My code so far is:
// Note the additional import of useEffect
import React, { useState, useEffect, useRef } from 'react';
import { TouchableOpacity, Button, Text, View, StyleSheet } from 'react-native';
import * as WebBrowser from 'expo-web-browser';
import Constants from 'expo-constants';
export default function App() {
useEffect(() => {
_handlePressButtonAsync();
}, []);
const [result, setResult] = useState(null);
const _handlePressButtonAsync = async () => {
let result = await WebBrowser.openBrowserAsync('https://vitech-104517.square.site/');
setResult(result);
};
return (
<View style={styles.container}>
<Button title="Open WebBrowser" onPress={_handlePressButtonAsync} />
<Text>{result && JSON.stringify(result)}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
});
I’m not sure how to import headlessjs or run headlessjs in my project. My project is using expo. How can I do this?
2
Answers
I recommend using BackgroundFetch from Expo. This api allows you to run tasks in background periodically.
Link to the Docs: https://docs.expo.dev/versions/latest/sdk/background-fetch/
I recommend using
expo-task-manager
which provides you with an API that allows you to manage long-running tasks, in particular, tasks that run while your app is running in the background. docs