react-native-background-job
react-native-background-job copied to clipboard
Tried to start task while in foreground
I'm new to react native, i coded it like this:
BackgroundJob.register({
jobKey: "userNotif",
job: async () => {
try {
const value = await AsyncStorage.getItem(USER_DATA);
if (value !== null) {
if (value !== 'anonymous') {
let parsedValue = JSON.parse(value);
await navigator.geolocation.getCurrentPosition(
async (position) => {
let userData = parsedValue;
let userLocation = {};
userLocation.latitude = position.coords.latitude;
userLocation.longitude = position.coords.longitude;
const timeInterval = await AsyncStorage.getItem(SELECTED_TIME_INTERVAL);
const reportRadius = await AsyncStorage.getItem(SELECTED_RADIUS);
const notificationAxios = axios.create({
baseURL: 'test-url',
});
await notificationAxios.post('test-url/reports/nearby', {
selected_radius: reportRadius ? parseFloat(reportRadius) : 0.5,
user_location: userLocation,
time_interval: timeInterval ? parseFloat(timeInterval) : 5,
})
.then((response) => {
if (response.data.status == 'success') {
if (response.data.reports.length > 0) {
let isSingular = response.data.reports.length == 1;
NotificationsAndroid.localNotification({
title: "Please Be Careful!",
body: "There " + ( isSingular ? 'is' : 'are' ) + " " + response.data.reports.length + " " + ( isSingular ? 'report' : 'reports' ) + " near your location!. Please be aware and if you feel something bad may happen, don't hesitate to call the police. Thanks!",
extra: {
time: MOMENT(),
}
});
}
}
})
.catch((error) => {
console.log(JSON.stringify(error));
});
},
(error) => {
console.log(JSON.stringify(error));
},
{enableHighAccuracy: true, timeout: 20000}
);
}
}
} catch (error) {
console.log(error);
}
}
});
export default class VeevMobile extends Component {
componentDidMount() {
BackgroundJob.schedule({
jobKey: 'userNotif',
// period: 300000,
period: 30000,
period: 10000,
timeout: 5000,
persist: true
});
}
}
All the time, after closing my app it crashes and stops. Then when I opened my app, i see this error. How can i fix this one?
Just another question, I am testing my app on real device, when i open the app and after the background job has been registered, i tried clicking the android home button which makes me go to Android Home. it doesnt notify me even if its on the recent list of apps. Is it because of my device? or something with the jobs?
Hi! We have tests running for this scenario so it shouldn't happen. But it could be a vendor specific issue though. What version of RN, react-native-background-jobs are you running?
@vikeri Im using "react-native": "0.46.1" and "react-native-background-job": "2.0.0"
Weird, what phone and Android version are you using?
tors 31 aug. 2017 kl 17:12 skrev sircEUNHO [email protected]:
Im using "react-native": "0.46.1" and "react-native-background-job": "2.0.0"
— You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/vikeri/react-native-background-job/issues/52#issuecomment-326326946, or mute the thread https://github.com/notifications/unsubscribe-auth/AAzrYK9Ab4i9j13FCWFAXNz0Qq94lBghks5sds12gaJpZM4PHw87 .
@vikeri I am using Genymotion Emulator, Google Nexus 7 2013 - 4.4.4 - api 19, also tried this on other devices on emulator with higher api but it still errors like this
Ok, haven't tested on Genymotion, only the default Android Studio emulator and real devices.
Could you try the following with a running emulator:
- Clone this repo:
git clone [email protected]:vikeri/react-native-background-job.git
-
cd react-native-background-job
- Install the example deps:
cd example && yarn
- Install the example app:
cd android && ./gradlew installDebug
- Run the tests:
cd ../.. && tests/assure-not-foreground.sh
I keep having this problem with 1.2.4-0 in production
Did you manage to replicate this in the tests? Any reason you're not using 2.X.X
?
Having the same problem.
@ritz078 For which version?
v2.0.1 . And the crashes are very much random. Once I have set allowInForeground, it seems to be working fine. Still, need some time to see if that has actually fixed that.
I have gotten these crashes now as well. Will investigate more, the problem is that it happens extremely rarely so it's very hard to test. If anyone can come up with a reproduction case of this in the emulator that would be extremely helpful.
I'm getting randomly these chrashes when I launch the app:
02-14 15:16:16.459 12734 12734 E AndroidRuntime: FATAL EXCEPTION: main 02-14 15:16:16.459 12734 12734 E AndroidRuntime: Process: com.valosolutions.ewa, PID: 12734 02-14 15:16:16.459 12734 12734 E AndroidRuntime: java.lang.IllegalStateException: Tried to start task myJob while in foreground, but this is not allowed. 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask(HeadlessJsTaskContext.java:92) 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at com.facebook.react.HeadlessJsTaskService$2.run(HeadlessJsTaskService.java:127) 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:761) 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:98) 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at android.os.Looper.loop(Looper.java:156) 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6523) 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 02-14 15:16:16.459 12734 12734 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Android 7.0 "react-native": "0.53.0", "react-native-background-job": "^2.1.1",
I am also seeing app crash with similar log as above.
java.lang.IllegalStateException: Tried to start task NotificationBgJobKey while in foreground, but this is not allowed.
at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask(HeadlessJsTaskContext.java:95)
at com.facebook.react.HeadlessJsTaskService$2.run(HeadlessJsTaskService.java:125)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Android 8.1.0 - Android Emulator "react-native": "0.57.8", "react-native-background-job": "2.2.0",
BackgroundJob.schedule({
period: 15000
});
Its happening when I run
react-native run-android
Will changing Utils.isReactNativeAppInForeground() to something similar to isAppOnForeground help ? https://facebook.github.io/react-native/docs/headless-js-android.html
Thanks
Keeping track of Android application/activity state on Android used to be a mess. It is seemingly trivial now with the new "lifecycle" architecture library
https://stackoverflow.com/a/49297230/9910298
Anyone tried this - in your application register a lifecycle listener that keeps track of start/stop state then in your service check it prior to executing?
The alternative would be something more involved like this https://stackoverflow.com/a/15573121/9910298 that nevertheless doesn't seem to handle the possible foreground states of BroadcastReceivers handling onReceive() or Services handling onCreate/onStart/onDestroy
The facebook example snippet above is a complicated and incomplete way to do it according to stack overflow - I should probably propose (and defend, maybe I am wrong...) a PR to their docs