Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background service on react-native android

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.

like image 215
andri Avatar asked Jan 08 '16 22:01

andri


People also ask

What is HeadlessJS in React Native?

Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example, to sync fresh data, handle push notifications, or play music.


2 Answers

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.

like image 162
Sriraman Avatar answered Sep 22 '22 18:09

Sriraman


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.

like image 34
billmalarky Avatar answered Sep 25 '22 18:09

billmalarky