1

I need to get a solution for Background job schedule. My purpose is to send latitude and longitude to my back end in every 15 minutes. What is the best way to achieve this functionality?

Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
Tanumay Ghosh
  • 73
  • 1
  • 8
  • did you try anything? – Learning Always Dec 27 '17 at 11:33
  • Please check this answer https://stackoverflow.com/questions/35376690/how-can-i-run-background-tasks-in-react-native?answertab=active#tab-top – nish Dec 27 '17 at 12:01
  • Thanks for your answer. Actually I have checked the question already. There are two libraries I can find from web. One is background-job and another is background-timer. But both are not working properly. They have so many issues still. I am still searching for proper solution for this case, also, if you can find any solution for this functionality, please share once. – Tanumay Ghosh Dec 27 '17 at 12:21
  • @JJG I have tried with two libraries as I mentioned in my previous command. Both are not working properly. If you have any solution, please share that. Thank you in advance. – Tanumay Ghosh Dec 27 '17 at 12:23

1 Answers1

2

I had the exact specification months ago and I concluded with 2 approach.

While my app requires active tracking and dispatch latlong object to my backend whenever there is a new latlong object coming in, one of the best options you can look for is HeadlessJS, some native coding is required.

My solution

All Javascript execution takes place on a background thread

Separate your business logic away from UI Thread and store them in redux. As long as the app is alive, it is entirely possible to dispatch an action every n minutes. Make sure you turn on the background mode in the Capabilities panel (for iOS).

Another options that I've tried

BackgroundTimer allows you to execute a particular snippet every n minutes. For example,

export const watchGPSPositionsAtInterval = function(
  dispatchNewPositionFunction,
  dispatch
) {
  let intervalId = BackgroundTimer.setInterval(() => {
    navigator.geolocation.getCurrentPosition(
      pos => {
        if (pos <= MIN_ACCURACY) {
          dispatchNewPositionFunction(pos, dispatch);
        }
      },
      error => {
        console.log(error);
      },
      {
        enableHighAccuracy: true,
        timeout,
        maximumAge
      }
    );
  }, TIME_INTERVAL);
  return intervalId;
};
Rex Low
  • 2,069
  • 2
  • 18
  • 47
  • Thanks for your answer. But I cannot understand, How to schedule this task in a particular timeout. Is there any library where I can schedule my task in a particular time interval? – Tanumay Ghosh Dec 28 '17 at 07:31
  • Have you tried running an example program, at all? If yes, please update your post and show us which part of the code is not working. – Rex Low Dec 28 '17 at 08:53
  • Hello @Rex, Sorry for so late reply. I have tried your example. It is working fine now. But it will not running the task when the app is killed. Is there any improvement in this code to achieve the functionality?? – Tanumay Ghosh Jun 04 '18 at 06:56
  • @TanumayGhosh I believe you have to mess with native code for that matter. – Rex Low Jun 04 '18 at 06:58
  • At last I got a solution. react-native-background-job fulfilled my need. Thanks Rex for your support. – Tanumay Ghosh Jun 04 '18 at 10:21
  • @TanumayGhosh but with notifications showed, right? – felansu Jul 03 '20 at 15:56