Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import BackgroundFetch from "react-native-background-fetch";
- componentDidMount = () => {
- BackgroundFetch.configure({
- minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
- // Android options
- stopOnTerminate: false,
- startOnBoot: true,
- requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default
- requiresCharging: false, // Default
- requiresDeviceIdle: false, // Default
- requiresBatteryNotLow: false, // Default
- requiresStorageNotLow: false // Default
- }, () => {
- 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\
- fetch('https://xxx.php',
- {
- method: 'POST',
- headers:
- {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify(
- {
- sales: 'sales',
- lat: this.state.currentLatitude,
- lon: this.state.currentLongitude,
- // bat: this.state.deviceInfo,
- bat: deviceInfo1[2]
- })
- }).then((response) => response.json()).then((responseJsonFromServer) => {
- }).catch((error) => {
- console.error(error);
- });
- BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
- }, (error) => {
- console.log("[js] RNBackgroundFetch failed to start");
- });
- // Optional: Query the authorization status.
- BackgroundFetch.status((status) => {
- switch (status) {
- case BackgroundFetch.STATUS_RESTRICTED:
- console.log("BackgroundFetch restricted");
- break;
- case BackgroundFetch.STATUS_DENIED:
- console.log("BackgroundFetch denied");
- break;
- case BackgroundFetch.STATUS_AVAILABLE:
- console.log("BackgroundFetch is enabled");
- break;
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement