Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'react-native-background-fetch' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
}, async () => {
console.log('- BackgroundFetch start');
let location = await BackgroundGeolocation.getCurrentPosition({persist: true, samples:1, extras: {'context': 'background-fetch-position'}});
console.log('- BackgroundFetch current position: ', location) // <-- don't see this
BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
}, (error) => {
console.log('[js] RNBackgroundFetch failed to start')
let BackgroundFetchHeadlessTask = async (event) => {
console.log('[BackgroundFetch HeadlessTask] start');
// Important: await asychronous tasks when using HeadlessJS.
/* DISABLED
let location = await BackgroundGeolocation.getCurrentPosition({persist: false, samples: 1});
console.log('- current position: ', location);
// Required: Signal to native code that your task is complete.
// If you don't do this, your app could be terminated and/or assigned
// battery-blame for consuming too much time in background.
*/
console.log('[BackgroundFetch HeadlessTask] finished');
BackgroundFetch.finish();
}
() => {
console.log('[js] Received background-fetch event');
// Required: Signal completion of your task to native code
// If you fail to do this, the OS can terminate your app
// or assign battery-blame for consuming too much background-time
BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
},
error => {
schedule: function(options = {}) {
// Cancel existing tasks
BackgroundFetch.stop()
// Configure the native module
// Automatically calls RNBackgroundFetch#start
BackgroundFetch.configure(
{ stopOnTerminate: false },
this._definition,
() => {
console.warn(`Background Fetch failed to start`)
}
)
},
configureBackgroundFetch() {
// [Optional] Configure BackgroundFetch.
BackgroundFetch.configure({
minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
stopOnTerminate: false, // <-- Android-only,
startOnBoot: true, // <-- Android-only
enableHeadless: true,
requiresCharging: false,
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE,
requiresDeviceIdle: false,
requiresBatteryNotLow: false,
requiresStorageNotLow: false
}, async () => {
console.log('- BackgroundFetch start');
let location = await BackgroundGeolocation.getCurrentPosition({persist: true, samples:1, extras: {'context': 'background-fetch-position'}});
console.log('- BackgroundFetch current position: ', location) // <-- don't see this
BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
}, (error) => {
console.log('[js] RNBackgroundFetch failed to start')
componentDidMount() {
//AppState.addEventListener('change', this.onAppStateChange.bind(this));
this.setState({
enabled: false
});
BackgroundFetch.configure({
stopOnTerminate: false,
minimumFetchInterval: 0
}, () => {
this.bgService.getPlugin().logger.ok('FETCH RECEIVED');
BackgroundFetch.finish();
}, (error) => {
console.warn('Fetch error: ', error);
});
this.bgService.getState((state) => {
this.configureBackgroundGeolocation(state);
});
// Fetch current app settings state.
this.settingsService.getState((state) => {
this.setState({
static configureBackgroundFetch() {
BackgroundFetch.configure(
{
minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
stopOnTerminate: false, // <-- Android-only,
startOnBoot: true, // <-- Android-only
},
() => {
console.log('[js] Received background-fetch event');
// Required: Signal completion of your task to native code
// If you fail to do this, the OS can terminate your app
// or assign battery-blame for consuming too much background-time
BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
},
error => {
console.log('[js] RNBackgroundFetch failed to start');
console.log(error);
},
setup() {
BackgroundFetch.configure(
{},
() => {
this.store.dispatch(TriggersActions.backgroundFetch())
},
error => {
// @todo - handle error
console.info('backgroundFetch error')
}
)
}
}
// Important: await asychronous tasks when using HeadlessJS.
/* DISABLED
let location = await BackgroundGeolocation.getCurrentPosition({persist: false, samples: 1});
console.log('- current position: ', location);
// Required: Signal to native code that your task is complete.
// If you don't do this, your app could be terminated and/or assigned
// battery-blame for consuming too much time in background.
*/
console.log('[BackgroundFetch HeadlessTask] finished');
BackgroundFetch.finish();
}
// Register your BackgroundFetch HeadlessTask
BackgroundFetch.registerHeadlessTask(BackgroundFetchHeadlessTask);
configureBackgroundFetch() {
// [Optional] Configure BackgroundFetch.
BackgroundFetch.configure({
minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
stopOnTerminate: false, // <-- Android-only,
startOnBoot: true, // <-- Android-only
enableHeadless: true,
requiresCharging: false,
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE,
requiresDeviceIdle: false,
requiresBatteryNotLow: false,
requiresStorageNotLow: false
}, async () => {
console.log('- BackgroundFetch start');
let location = await BackgroundGeolocation.getCurrentPosition({persist: true, samples:1, extras: {'context': 'background-fetch-position'}});
console.log('- BackgroundFetch current position: ', location) // <-- don't see this
BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
}, (error) => {
console.log('[js] RNBackgroundFetch failed to start')
});
}