React Native Environment Info:
System:
OS: Windows 10
CPU: (4) x64 Intel(R) Core(TM) i5-3337U CPU @ 1.80GHz
Memory: 860.27 MB / 3.89 GB
Binaries:
Yarn: 1.12.3 - C:\Program Files (x86)\Yarnbin\yarn.CMD
npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
IDEs:
Android Studio: Version 3.2.0.0 AI-181.5540.7.32.5056338
react: "16.6.3",
react-native: "0.57.8",
I'm looking for a way to run task or service when the app is killed (not in background state), I tried to use Headless JS but it's working in foreground only, then I tried the JobScheduler but it doesn't seem work when app is killed.
My task is about fetching data from server and push notification in some conditions.
Any help please.
you cannot run anything when app is killed.
@RittaIbrahim did you get Headless running while app in background ?
can you share any code base for it.
Same issue here. I disagree with @dulmandakh that you cannot run anything.
After I kill the app, then send it a push notification, I can see in logcat that the app wakes up and Firebase handles the notification. I also want to let JS handle the notification. Then the app crashes because of the following: https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten/49846410#49846410.
Apparently, the background system changed in newer versions of Android and this has to be updated in RN.
It seems like HeadlessJsTaskService needs to be updated to use Job / JobScheduler https://developer.android.com/reference/android/app/job/JobScheduler.html
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () =>
console.log('bgMessaging: ', new Date()),
);
Headless works when the app is in the background but not when app is killed
@RittaIbrahim Headless JS seamlessly works when the app is killed i.e even when it is not in background. I had to handle In-line reply for push notification in my android app and Headless JS was the perfect solution for it. The documentation is very neat provided by them and it doesn't. Try setting to the timeout to 0 sec or min as required by your task and that should work fine.
import {AppRegistry} from 'react-native'; import App from './App'; import {name as appName} from './app.json'; AppRegistry.registerComponent(appName, () => App); AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => console.log('bgMessaging: ', new Date()), );Headless works when the app is in the background but not when app is killed
The above code doesn't work.
@RittaIbrahim Headless JS seamlessly works when the app is killed i.e even when it is not in background. I had to handle In-line reply for push notification in my android app and Headless JS was the perfect solution for it. The documentation is very neat provided by them and it doesn't. Try setting to the timeout to 0 sec or min as required by your task and that should work fine.
Neat? Seriously? The RN documentation for Headless JS is one of the worse I have come across.
Most helpful comment
@RittaIbrahim did you get Headless running while app in background ?
can you share any code base for it.