Advertisement
Eko_Sariyanto

Untitled

Mar 25th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import BackgroundFetch from "react-native-background-fetch";
  2.  
  3.  
  4. componentDidMount = () => {
  5.  
  6. BackgroundFetch.configure({
  7. minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
  8. // Android options
  9. stopOnTerminate: false,
  10. startOnBoot: true,
  11. requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default
  12. requiresCharging: false, // Default
  13. requiresDeviceIdle: false, // Default
  14. requiresBatteryNotLow: false, // Default
  15. requiresStorageNotLow: false // Default
  16. }, () => {
  17. console.log("[js] Received background-fetch event");
  18. // Required: Signal completion of your task to native code
  19. // If you fail to do this, the OS can terminate your app
  20. // or assign battery-blame for consuming too much background-time\
  21. fetch('https://xxx.php',
  22. {
  23. method: 'POST',
  24. headers:
  25. {
  26. 'Accept': 'application/json',
  27. 'Content-Type': 'application/json',
  28. },
  29. body: JSON.stringify(
  30. {
  31. sales: 'sales',
  32. lat: this.state.currentLatitude,
  33. lon: this.state.currentLongitude,
  34. // bat: this.state.deviceInfo,
  35. bat: deviceInfo1[2]
  36. })
  37.  
  38. }).then((response) => response.json()).then((responseJsonFromServer) => {
  39. }).catch((error) => {
  40. console.error(error);
  41. });
  42.  
  43. BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
  44. }, (error) => {
  45. console.log("[js] RNBackgroundFetch failed to start");
  46. });
  47.  
  48. // Optional: Query the authorization status.
  49. BackgroundFetch.status((status) => {
  50. switch (status) {
  51. case BackgroundFetch.STATUS_RESTRICTED:
  52. console.log("BackgroundFetch restricted");
  53. break;
  54. case BackgroundFetch.STATUS_DENIED:
  55. console.log("BackgroundFetch denied");
  56. break;
  57. case BackgroundFetch.STATUS_AVAILABLE:
  58. console.log("BackgroundFetch is enabled");
  59. break;
  60. }
  61. });
  62.  
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement