react-native-background-task | Periodic background tasks for React Native apps | iOS library
kandi X-RAY | react-native-background-task Summary
kandi X-RAY | react-native-background-task Summary
Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Schedules a background task
- Commit the scheduled job
- This is called when a pause occurs
- Sets the foreground
- Invoked when a job is running
- Initializes the ReactApplicationContext
- Returns the task config for the task
- Cancels the currently scheduled task
- Creates the native modules
- Resume all the active jobs
- Create the modules
- Create view managers
- Sets up the native job creator
react-native-background-task Key Features
react-native-background-task Examples and Code Snippets
import React from 'react'
import { Text } from 'react-native'
import BackgroundTask from 'react-native-background-task'
BackgroundTask.define(() => {
console.log('Hello from a background task')
BackgroundTask.finish()
})
class MyA
import React from 'react'
import { Text } from 'react-native'
import BackgroundTask from 'react-native-background-task'
import { Notifications, Permissions, Constants } from 'expo';
BackgroundTask.define(async () => {
// if time is 1
import React from 'react'
import { Text } from 'react-native'
import BackgroundTask from 'react-native-background-task'
BackgroundTask.define(() => {
console.log('Hello from a background task')
BackgroundTask.finish()
})
class MyA
Community Discussions
Trending Discussions on react-native-background-task
QUESTION
I'm building a new app with React Native and Expo. The main function of this app is to generate plans for user. The generating algorithm will take estimated around 300-500 seconds to execute. (It contains maximum 2,100,000 times of random generating.)
Currently I am facing two problems:
Q1. during testing, this piece of code is put in the App.js - App class - render function directly. however, it seems that if the execution time exceeds 32 seconds the app will just fail to render and stay no response forever.
So the question is:
1.1 what's the reason of the maximum 32 seconds otherwise no response?
1.2 are there better ways to do testing?
Current testing method:
...ANSWER
Answered 2020-Mar-03 at 13:16Well, I don't think there is one definite answer for your question, But here is what i think:
- I'm not sure about any 32 seconds render limit but blocking the UI for even 1 second is not practical and bad for user experience.
- It is not a good practice to to perform anything inside the
render()
method. This method is called frequently whenever a piece of the state is updated or the component is re-rendered. So it should be as light as possible and should only contain the returned components, Also to avoid any unexpected behaviors therender()
method should be a pure method. - What i would do in this case is re-writing the calculation algorithm in C++ then create a bridge between React Native and Android/iOS where i can run the code on a thread beside the UI thread then return the value to the JavaScript part of the app.
- Using C++ in heavy calculations should give you a boost in performance and will help you keep your algorithm in one place (For easier maintenance and debugging).
- You can also write the algorithm twice using Java/Kotlin for Android and Objective-C for iOS, This method is useful to avoid any C++ code complications but again you're writing the same code twice in different languages (So you'll face other complications).
- Check out this article to learn more about Native Modules/Bridges.
- Also check out this article to get information about C++ for Android.
- Another way to solve this problem is to move the algorithm to a centralized server where you calculate the values and store it on the server then retrieve it using some kind of web api.
- Now if you want (for some reason) to keep your algorithm in JavaScript, You'll have to break the calculation process into tiny and lightweight pieces then
requestAnimationFrame
for every piece, For example if your algorithm generates a random number then does some calculation on it and repeats this process for 2 million times, You'll need torequestAnimationFrame
for every calculation, store the current result somewhere and request another frame for the next calculation.- We've to do this because by contract JavaScript has only one thread and all JS code including rending frames on the screen happens on this thread so we break our code into small pieces to allow for new frames to be rendered in between.
- Check out this article to learn more about dividing workloads and animation frames.
Good Luck
QUESTION
I'm trying to do in my React Native App that each X min the app fetch some data of my application included if the app is closed. Or more similar, each X min I want to save a value in the AsyncStorage
.
It suppose that the react-native-background-task
does that but, unfortunately, the project is abandoned with a lot of errors and bugs that make using it unviable.
Is there any other way to do it?
...ANSWER
Answered 2020-Feb-01 at 22:28Have you tried react-native-queue
.I think it will suit your requirement.
Click here For complete guide.
You can try react-native-background-job
too.
QUESTION
I am using react-native, redux-saga, react-native-background-task.
First, when my app goes from the foreground to the background (on both IOS and Android), saga continues. I would have expected it to stop, but I guess because saga is on another thread it continues. Is this going to cause issues for battery management or prevent IOS from allowing to app to execute background fetch events?
Second, when a background task starts, on the Android app my main saga starts like normal (not expected behavior, I would have thought only the background task code to run. On IOS, only the background task code runs as I would have expected. Why does the Android app start the whole saga again?
Third, when I use xcode to 'simulate background fetch' it was working for a little bit, but now it is closing whatever other app is open and not running the task code. Why is this?
Here is where I define my background task:
...ANSWER
Answered 2019-Aug-23 at 19:34When a react native app is backgrounded on android it doesn't mean the activity is necessarily destroyed so your js code may continue to run for an indeterminate amount of time. Eventually it's likely your activity will be stopped if the user doesn't open it again but you can't know exactly when. You could use react native's AppState
(https://facebook.github.io/react-native/docs/appstate) to cancel sagas that you wish cancelled when the app goes into the background.
For the background task question, the implementation on android for BackgroundTask uses react native's headless js (https://facebook.github.io/react-native/docs/headless-js-android.html) which starts your entire app in the background. On ios there is no headless js so the implementation is different.
QUESTION
Both React Native Video and timer (used setInterval) are worked on foreground and timer is stopped when app is in the background. But still video plays in the background.
It means, when the application pauses, so do all setInterval's running and (and setTimeouts pending).
headlessJs api is available only for android to run the task in background.
If I use headlessJs, Isn't product pain for react native to run a timer?
There are some desired libraries,
react-native-background-job - Use headlessJs and work only in android.
react-native-background-task - Use headlessJs in android and ios uses a proxy around react-native-background-fetch library (Support only for a single task,The exact timings of task execution are unpredictable)
...Can I use setInterval in the background service if I use those libraries?
Why only music player is working in the background?
ANSWER
Answered 2019-Jul-02 at 02:16I found a solution from react-native-background-timer library.
In iOS, we can start the timer or do any react native stuff in the background after execute the following code.
Background timer works even we use other apps in the device. (Ex: itune, whatsup)
QUESTION
I need to send HealthKit data to a server periodically. I'm using React Native and rn-apple-healthkit
. I tried react-native-background-task
, but it won't work while the application is closed (not background mode). I read in Apple developer docs that it happens by design.
I found some solutions online such as geolocation services, running a socket and "playing" silent sounds, piggybacking on push notifications but they all seem like workarounds.
Is there a good way to do this in 2019?
...ANSWER
Answered 2019-Apr-04 at 12:35Using silent notifications is the correct way of achieving this. However it wont work when the phone is locked:
For security, the HealthKit store is encrypted when the device is locked, and the HealthKit store can only be accessed by an authorized app. As a result, you may not be able to read data from the store when your app is launched in the background; however, apps can still write data to the store, even when the phone is locked.
Taken from https://developer.apple.com/documentation/healthkit/protecting_user_privacy
QUESTION
I'm getting following error while running app for android
...ANSWER
Answered 2018-Jun-13 at 09:57Here you are using same dependencies two times in 1st line and in 4th last line remove one from here. don't use same dependencies for multiple times
QUESTION
ANSWER
Answered 2018-May-15 at 09:03Edit android/app/build.gradle
use this
compile ("com.facebook.react:react-native:0.44.0") { force = true }
and write version of app you use instead of 0.44.0 and delete something like this compile "com.facebook.react:react-native:+" or this compile "com.facebook.react:react-native:0.44.0"
QUESTION
I am trying to sync data capture offline with an online api, I periodically run an background task using react-native-background-task to retrieve offline data and sync the data with an online api.
...ANSWER
Answered 2018-Apr-25 at 20:32I fixed it, it turned out react-native-background-task version wasn't compatible with my react-native version, i upgraded from 0.48.1 to 0.51.0 which requires react 16.0.0
QUESTION
I have a react-native application which includes react-native-background-task which uses react-native-background-fetch. I configured a job to console.log a simple message when it runs. I understand that the background fetch is not working on simulator, but you can test it by going into Debug -> Simulate background fetch
. When I hit this, the app goes to background. I did not get any output. I have enabled background fetch in Info.plis and also the code returns status.available when I check with the library function.
How do you actually test this?
ANSWER
Answered 2017-Sep-22 at 07:13Since you've mentioned that you've tried writing to storage also - which is not working - my updated answer would be: while you are running background fetch, you can at the same time hit a server on which you can see the logs. That way, you can tell, if the background fetch was successfully working.
I've created a hackable localTestingServer that you can fork and get started immediately.
QUESTION
I am building an app for ios in react native.
Using the react-native-background-task I am running a task that fetches data from a server.
On IOS the default and lowest period is 15 minutes, which is too high for what I need.
Is there a way to create a background task on IOS with a lower period?
ANSWER
Answered 2017-Jun-17 at 22:22The short answer is you can't.
iOS does not support frequently polling in the background as it is not battery or network friendly.
That module uses the iOS background fetch service which is the only supported background fetch facility on iOS.
With background fetch, iOS invokes you background fetch handler when it determines. This may be even less frequently than 15 minutes; the time interval you supply is a requested interval only and will be modified based on how often and when you server returns new data and other factors.
The preferred approach is to use a push notification when your server has new data.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install react-native-background-task
You can use react-native-background-task like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the react-native-background-task component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page