Comments (18)
Hi @Rewieer, thank you for your observation.
@ericacooksey fixed it in #17 but on my devices everything worked well also with previous version (0.1.3).
I just released 0.1.4-beta so please try npm i react-native-background-timer@beta --save
and let me know if it works properly when phone is locked.
Many thanks.
from react-native-background-timer.
I'm sorry, I cannot try your solution for now as I've finally implemented my own background-timer system. If you want to know, I'm using a wakelock that is acquired when the module is loaded (i'm using intervals).
I want to point that using sony phones, when stamina mode is on (or what the setting to disallow applications to fetch datas while in sleep mode) disable using a background timer. I've been tricked for the whole week-end on this. You should maybe talk about it in the README to avoid further problems on these phones.
from react-native-background-timer.
When the phone is locked and the screen is off the events don't fire. I am using 0.1.4 (beta) as suggested.
from react-native-background-timer.
@MaximilianKindshofer do you want me to share the solution ? It requires a bit of knowledge in Java to implement through
from react-native-background-timer.
This would be great!
I am not so skilled at Java - but I am sure I can dive into it
from react-native-background-timer.
It's pretty minimal for my purpose.
The module looks like this :
import android.os.Handler;
import android.os.PowerManager;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import static android.content.Context.POWER_SERVICE;
public class BackgroundTimerModule extends ReactContextBaseJavaModule {
private PowerManager powerManager;
private PowerManager.WakeLock wakeLock;
public BackgroundTimerModule(ReactApplicationContext reactContext){
super(reactContext);
this.powerManager = (PowerManager) getReactApplicationContext().getSystemService(POWER_SERVICE);
this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "rewieer_bg_wakelock");
this.wakeLock.acquire();
}
@Override
public String getName() {
return "RewieerBackgroundTimerModule";
}
@ReactMethod
public void setTimeout(final int id, final int timeout){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(getReactApplicationContext().hasActiveCatalystInstance()){
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("rewieer.backgroundTimer.timeout", id);
}
}
}, timeout);
}
}
It basically acquire a partial wakelock permanently to ensure CPU is not sleeping.
Then in JS :
import { NativeModules, DeviceEventEmitter } from 'react-native';
const NativeEVOBackgroundTimer = NativeModules.RewieerBackgroundTimerModule;
const listeners = {};
let uniqueId = 0;
const nextId = () => uniqueId++;
const RewieerBackgroundTimer = {
setInterval(callback, delay){
let id = nextId();
listeners[id] = {
isInterval: true,
callback,
timeout: delay,
};
NativeEVOBackgroundTimer.setTimeout(id, delay);
return id;
},
clearInterval(id){
listeners[id] && delete listeners[id];
}
};
DeviceEventEmitter.addListener("rewieer.backgroundTimer.timeout", (id) => {
const listener = listeners[id];
if(!listener){
return;
}
listener.callback();
if(listener.isInterval){
NativeEVOBackgroundTimer.setTimeout(id, listener.timeout);
return;
}
delete listeners[id];
});
export default RewieerBackgroundTimer;
The interface is the same as RNBT but it doesn't provide any setTimeout function, because I don't need it. You can implement it easily through, just copy the code of RNBT.
If you have no idea on how to implement this head to the react native doc.
Don't forget to add <uses-permission android:name="android.permission.WAKE_LOCK" />
to your manifest.
This solution can certainly be discussed and improved, as it drains the battery life when used heavily.
from react-native-background-timer.
Yes I can see that this will be really battery heavy - meanwhile I found:
https://github.com/vikeri/react-native-background-job
Utilizing the https://developer.android.com/reference/android/app/job/JobScheduler.html
But this wont work in foreground
from react-native-background-timer.
You might actually implement an interface and switch given the context.
For my project I have a BaseComponent React Element. If the app is in background, it renders BackgroundComponent. If the app is in foreground, it renders ForegroundComponent. Each use different kind of periodic methods.
from react-native-background-timer.
For my work I need to do repetitive task every 30 seconds. I've looked at JobSchedulers but it's more like a way to synchronize your application. I don't think your setInterval method has an interval of 15 minutes :p
from react-native-background-timer.
@ocetnik Is a chance to resolve this problem?
I have noticed when app is running by react-native run-android a timer still working even when phone is locked. But when app has been installed by .apk file then timer is stopped after 30-40 seconds (when phone is locked).
from react-native-background-timer.
@irekrog there might be many causes to this and I must admit i'm as well struggling with such problems.
If the timer stop workings when phone is locked, it's a Wakelock problem.
from react-native-background-timer.
rn can't run task on background, it can be killed when CPU dormancy, I use Android native run background task and emit event to rn listener .....
https://facebook.github.io/react-native/docs/native-modules-android.html
from react-native-background-timer.
Well RN is perfectly capable of doing this with a bit of native code, at least under android. That's what HeadlessJS do. A wakelock is sufficient to keep the CPU awake for background tasks, but it's resource consuming.
If it's about fetching notification like every hour or so, AlarmManager fits perfectly.
from react-native-background-timer.
Issue still present on Android. Is there a solution?
from react-native-background-timer.
hey @sindhu732 i have added the solution given by @Rewieer , check this Link
from react-native-background-timer.
It's pretty minimal for my purpose.
The module looks like this :import android.os.Handler; import android.os.PowerManager; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.modules.core.DeviceEventManagerModule; import static android.content.Context.POWER_SERVICE; public class BackgroundTimerModule extends ReactContextBaseJavaModule { private PowerManager powerManager; private PowerManager.WakeLock wakeLock; public BackgroundTimerModule(ReactApplicationContext reactContext){ super(reactContext); this.powerManager = (PowerManager) getReactApplicationContext().getSystemService(POWER_SERVICE); this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "rewieer_bg_wakelock"); this.wakeLock.acquire(); } @Override public String getName() { return "RewieerBackgroundTimerModule"; } @ReactMethod public void setTimeout(final int id, final int timeout){ Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { if(getReactApplicationContext().hasActiveCatalystInstance()){ getReactApplicationContext() .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit("rewieer.backgroundTimer.timeout", id); } } }, timeout); } }
It basically acquire a partial wakelock permanently to ensure CPU is not sleeping.
Then in JS :import { NativeModules, DeviceEventEmitter } from 'react-native'; const NativeEVOBackgroundTimer = NativeModules.RewieerBackgroundTimerModule; const listeners = {}; let uniqueId = 0; const nextId = () => uniqueId++; const RewieerBackgroundTimer = { setInterval(callback, delay){ let id = nextId(); listeners[id] = { isInterval: true, callback, timeout: delay, }; NativeEVOBackgroundTimer.setTimeout(id, delay); return id; }, clearInterval(id){ listeners[id] && delete listeners[id]; } }; DeviceEventEmitter.addListener("rewieer.backgroundTimer.timeout", (id) => { const listener = listeners[id]; if(!listener){ return; } listener.callback(); if(listener.isInterval){ NativeEVOBackgroundTimer.setTimeout(id, listener.timeout); return; } delete listeners[id]; }); export default RewieerBackgroundTimer;
The interface is the same as RNBT but it doesn't provide any setTimeout function, because I don't need it. You can implement it easily through, just copy the code of RNBT.
If you have no idea on how to implement this head to the react native doc.Don't forget to add
<uses-permission android:name="android.permission.WAKE_LOCK" />
to your manifest.This solution can certainly be discussed and improved, as it drains the battery life when used heavily.
As you wrote setTimeOut, how can I implement SetTimeInterval in same way?
from react-native-background-timer.
@GhayoorUlHaq just call setTimeout again once the function times out.
from react-native-background-timer.
@rewieer actually I have to execute a some lines of code after specific time period that will be defined by user and those lines should execute periodically even app is in background and phone is locked unless user stop that.
from react-native-background-timer.
Related Issues (20)
- BackgroundTimer stops when app goes to background after Few Sec in IOS React Native IOS HOT 18
- runBackgroundTimer doesn't work on RN 66.3 - Tested on Android HOT 5
- Background service is not working if Background data usage disabled. HOT 1
- Using react-native-background-timer with react-native-web HOT 1
- Terminating app due to uncaught exception NSInternalInconsistencyException HOT 1
- how to get clipboard content on background service
- Testing background timer with useFakeTimers in Jest HOT 4
- Background Action and Graphql HOT 2
- the task is dying out HOT 8
- background task dosen't work when device is in sleep mode. HOT 7
- Is this repo still alive? HOT 5
- BackgroundTimer.clearTimeout does not stop ongoing background task
- Invariant Violation: new NativeEventEmitter() requires a non-null argument., js engine: hermes HOT 1
- react-native-background-timer not working in android HOT 1
- after upgrading gradle version to 8.2.2 I'm getting this error: Setting the namespace via the package attribute in the source AndroidManifest.xml is no longer supported. HOT 3
- React Native Background Task Termination on Device Lock in Android OS <= 13 HOT 1
- RangeError in long running timers HOT 1
- Problem build/config with MainApplication.kt HOT 1
- I am geting error like : setTimeout is null
- Lint error causing build fail - "InvalidWakeLockTag" HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-background-timer.