3

I am new to react-native and implementing simple app where app will monitor the mobile's clock time and should set some flags so as to notify user to perform some task when he starts the app.

I want to continuously monitor mobile's clock time as a background job in the react native app such that, when user opens the app and if specific time has already passed, it should set some flag to take future decision.

After some research over blogs, I found that react-native-background-task and/or background-timers can be used. But I am finding difficulty in implementing it. Can anyone help me with the implementation example for the same. Thank you in advance.

user10747870
  • 31
  • 1
  • 2
  • 3
    Possible duplicate of [What is the best way to schedule a task in react native?](https://stackoverflow.com/questions/39946431/what-is-the-best-way-to-schedule-a-task-in-react-native) – Maxime Apr 11 '19 at 07:41
  • I have already gone through the mentioned link for answers. Unfortunately could not come up with solution. I wanted some example code to understand. – user10747870 Apr 11 '19 at 08:17

1 Answers1

1

So, from your post I'm going to assume a few things. One being you'll develop on Android as well as iOS, you just want to use a plugin (not code a module yourself).

Unfortunately, unless I'm mistaken - there's no simple answer and I'll explain.

As Android and iOS go forward they are starting to limit usage of Apps in the background, specifically Android goes into 'Doze' mode and iOS works in a similar fashion.

For Android, you'll need to consider a few things and I'll concentrate on them, as you'll need to know this before actually creating a RN app.

Doze Mode

Starting from Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users by managing how apps behave when a device is not connected to a power source. Doze reduces battery consumption by deferring background CPU and network activity for apps when the device is unused for long periods of time. App Standby defers background network activity for apps with which the user has not recently interacted.

While the device is in Doze, apps' access to certain battery-intensive resources is deferred until maintenance windows. The specific restrictions are listed in Power Management Restrictions.

Doze and App Standby manage the behavior of all apps running on Android 6.0 or higher, regardless whether they are specifically targeting API level 23. To ensure the best experience for users, test your app in Doze and App Standby modes and make any necessary adjustments to your code. The sections below provide details.

Cruically, you'll need to note:

The system does not allow sync adapters to run.

The system does not allow JobScheduler to run.

So firstly for android, you'll (probably) need to ensure your app is in a 'whitelist'. You can check the requirements of the list here:

https://developer.android.com/training/monitoring-device-state/doze-standby

Or, you can access the maintenance window with a plugin like here:

https://github.com/transistorsoft/react-native-background-fetch

Testing

Android give you some tools to do so, mainly running:

$ adb shell dumpsys battery unplug
$ adb shell am set-inactive <packageName> true

Headless JS

You can check out headless tasks, which could suit your situation:

https://facebook.github.io/react-native/docs/headless-js-android

Caveats:

Although Android state that the operating system itself acts like documented, devices themselves can have software built in which essentially kills background processes. Nokia is one of the worst. So be aware of this.

Community
  • 1
  • 1
JRK
  • 3,686
  • 1
  • 13
  • 19