Hi guys!
I’m wondering how you would go about testing if background fetch is working on Android?
I’m creating a form based app, where the users will be going to remote locations. There will possibly be no network out there, so I need background fetch to wake the app up every so often so the data can be uploaded to Firebase when the user is back in civilization - and then turn off background fetch once that’s done.
I’m guessing background fetch is the way to go rather than background mode, but there’s not much information on it on the forum or stackoverflow.
I followed exactly what is outlined in the docs: https://ionicframework.com/docs/v3/native/background-fetch/
Although it says its only for iOS on the Ionic docs, the Github docs say Android too.
I opened the app, turned the phone on airplane mode to stop info from being sent to Firebase, filled out the form and pressed send. I then closed the app and turned off airplane mode, and after 20 minutes the data never arrived to Firebase.
To test if it was even working, I tried making a h1 visible once the background fetch callback happens. After 25 mins I opened the app and the h1 was still invisible.
Is there another step to this, or something I’m missing? And is there a simpler way of testing if it works?
My code:
home.ts
fetched:boolean = false;
constructor(public navCtrl: NavController, public fireStore: FireStoreProvider, private backgroundFetch: BackgroundFetch) {
const config: BackgroundFetchConfig = {
stopOnTerminate: false, // Set true to cease background-fetch from operating after user "closes" the app. Defaults to true.
};
backgroundFetch.configure(config)
.then(() => {
console.log('Background Fetch initialized');
this.fetched = true;
this.backgroundFetch.finish();
})
.catch(e => console.log('Error initializing background fetch', e));
backgroundFetch.start();
backgroundFetch.stop();
}
home.html
<ion-content padding>
<h1 *ngIf="fetched">Fetch has fired</h1>
</ion-content>