Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React-native-background-fetch

$ react-native unlink react-native-background-fetch
Comment

React-native-background-fetch

$ npm install --save react-native-background-fetch
Comment

React-native-background-fetch

import React 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";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      events: []
    };
  }

  componentDidMount() {
    // Initialize BackgroundFetch ONLY ONCE when component mounts.
    this.initBackgroundFetch();
  }

  async initBackgroundFetch() {
    // BackgroundFetch event handler.
    const onEvent = async (taskId) => {
      console.log('[BackgroundFetch] task: ', taskId);
      // Do your background work...
      await this.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);
  }

  // Add a BackgroundFetch event to <FlatList>
  addEvent(taskId) {
    // Simulate a possibly long-running asynchronous task with a Promise.
    return new Promise((resolve, reject) => {
      this.setState(state => ({
        events: [...state.events, {
          taskId: taskId,
          timestamp: (new Date()).toString()
        }]
      }));
      resolve();
    });
  }

  render() {
    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={this.state.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;
Comment

PREVIOUS NEXT
Code Example
Javascript :: js print objects 
Javascript :: script refresh js 
Javascript :: hide component vuejs 
Javascript :: find number in array js 
Javascript :: how to get time zone difference date-fns 
Javascript :: js console log 
Javascript :: what is next.js 
Javascript :: jest check if button is disabled 
Javascript :: javascript map replace key value 
Javascript :: $(...).DataTable is not a function 
Javascript :: js for await 
Javascript :: add webpack to react project 
Javascript :: jquery change label content 
Javascript :: string immutable javascript 
Javascript :: reisze image expo react native 
Javascript :: $(...).Datatables is not a function 
Javascript :: is date 1 day ago javascript 
Javascript :: Check Object Is Instance Of Class 
Javascript :: fetch thingy 
Javascript :: catch status code 403 in fetch 
Javascript :: utc clock 
Javascript :: how to control where the text cursor on div 
Javascript :: create object javascript 
Javascript :: array iterator javascript 
Javascript :: jsonArray find 
Javascript :: how to make chrome extension js 
Javascript :: sample promise.all javascript 
Javascript :: convert string to array with condition javascirpt 
Javascript :: set tiemzone datetime object 
Javascript :: service worker.js 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =