Background service on react-native android-ThrowExceptions

Exception or error:

Is there any way to create a background service with react-native on android?
I would like some sort of timer that wakes up every hour or so, and launches a simple javascript task.

How to solve:

Yes, It can be done.

React native operates on top of the native (java/Objc) to JS bridge with a concept of “native” and JS modules (modules can have methods that you may call from the “other” side of the bridge). All the UI stuff is built on top of the bridge (the main “native” module that handles generating views is called “UIManager”). It is possible to use bridge directly the only restriction is that the communication has to be asynchronous.

You can call the javascript function from the JAVA code. Check this link for the documentation.

###

Release v0.36 support headless-js, only Android, for now.

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

###

Absolutely. In fact it’s quite easy now to achieve this entirely in JS (no need to write native code) cross platform with react native queue and react native background task.

There are some limitations. The background task will only be fired roughly every 15 minutes at smallest intervals (and the timing isn’t guaranteed to be perfect if it even fires at all – the iOS/Android scheduler is sort of a black box that looks at battery life, current cpu load, etc, when determining when to fire a scheduled task). Also the task is limited to 30 seconds of execution.

I’ve written a tutorial on how to set all this up here.

Let me know if you have any difficulty getting it up and running.

###

It’s Working in my case user the react-native-background-job library for running a background service. It’s working after the kill the app.
https://github.com/vikeri/react-native-background-job

import BackgroundJob from "react-native-background-job";
const regularJobKey = "regularJobKey";

BackgroundJob.register({
  jobKey: regularJobKey,
  job: () => {
    
    console.log('Background Service Call!');

  }
});
<TouchableHighlight
  onPress={() => {
    BackgroundJob.schedule({
      jobKey: regularJobKey,
      period: 2000
    });
  }}
>
  <Text>Schedule regular job</Text>
</TouchableHighlight>

Example Here : https://github.com/vikeri/react-native-background-job/blob/master/example/index.android.js

Leave a Reply

Your email address will not be published. Required fields are marked *