javascript - Background service in React Native - Stack Overflow

admin2025-04-22  0

I'm using Firebase, while my app is in the background and i want to when the database has new data, the notification will be pushed, like this:

message.on('child_added', function(data) {
  pushNotification(data);
});

I have searched on Google and found this library: .

Everything works fine, my background job:

const newMessageJob = {
   jobKey: "newMessageJob",
   job: () => {
     pushNotification(newMessage);
   }
};
BackgroundJob.register(backgroundJob);

But the problem is here, it's only called at fixed times (looks like setInterval):

BackgroundJob.schedule({
  jobKey: "newMessageJob",
  period: 1000,
  timeout: 1000,
  exact: true
});

But i want it to work dynamically (Background Job fired when new data has been added), so how can i solve this problem?

Thanks.

I'm using Firebase, while my app is in the background and i want to when the database has new data, the notification will be pushed, like this:

message.on('child_added', function(data) {
  pushNotification(data);
});

I have searched on Google and found this library: https://github./vikeri/react-native-background-job.

Everything works fine, my background job:

const newMessageJob = {
   jobKey: "newMessageJob",
   job: () => {
     pushNotification(newMessage);
   }
};
BackgroundJob.register(backgroundJob);

But the problem is here, it's only called at fixed times (looks like setInterval):

BackgroundJob.schedule({
  jobKey: "newMessageJob",
  period: 1000,
  timeout: 1000,
  exact: true
});

But i want it to work dynamically (Background Job fired when new data has been added), so how can i solve this problem?

Thanks.

Share Improve this question asked Dec 10, 2017 at 17:03 Thanh MinhThanh Minh 581 gold badge2 silver badges6 bronze badges 4
  • This question is way too broad. If you're using redux, you may do this with redux-saga, for example, or just by making your own simple middleware. If you don't want anything plicated, you can just set callback in ponentDidMount and call setState. In react data travels only from parent to children, so you will need to lift this to the level where you can pass data as props to all ponents which need it. In many cases this would be topmost ponent. – aemxdp Commented Dec 10, 2017 at 20:36
  • Thanks, but i want to fire it while my app is in the background, and i'm using MobX, not Redux, how can i do that? – Thanh Minh Commented Dec 11, 2017 at 3:57
  • Ah ok, I misunderstood your problem initially. The first thing which es to my mind is to do polling: push messages to queue on arrival and schedule a background job to fire every N milliseconds checking if there are unprocessed items in queue. There may be better solutions though. – aemxdp Commented Dec 11, 2017 at 12:08
  • Here you can get relevant help stackoverflow./questions/34687473/… – Irshad Babar Commented Sep 19, 2020 at 20:02
Add a ment  | 

1 Answer 1

Reset to default 2

Periodic background tasks while the app is closed can be solved in pure JS (no native code needed) cross platform now with react native queue and react native background task, but what you are wanting to do (run an ongoing service in the background instead of a periodic task) is not possible in React Native currently (and there isn't even a general way to do this natively as far as I understand, apps use workarounds).

The current limitations on cross platform background tasks are as follows:

  • The smallest interval of time between task execution is ~15 min.
  • The background task has a timeout limit of 30 seconds.
  • The task schedule is approximate. iOS/Android use battery life, current cpu load, etc to determine if a scheduled task can be executed or if execution should be delayed or even cancelled. It will generally run when you want it to, but don't expect the timing to be exact.
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745297048a295035.html

最新回复(0)