简体   繁体   中英

IONIC2 - app gets killed after some time even when using Background Geolocation

I have an IONIC2 app, which needs to wake up every morning at 8 AM for 20 minutes to send user reminders based on the user's geolocation.

I am using this plugin (which uses IOS significant changes API to monitor changes in user's location) https://github.com/mauron85/cordova-plugin-background-geolocation

The problem: The app doesn't get killed when I close the app and the background geolocation works for me fine for some time. I have tested upto an hour. But when I wake up next morning, I find the app was killed by IOS.

I know there is another plugin to keep the app running in the background https://github.com/katzer/cordova-plugin-background-mode , but I have read tons of complaints from people that it will cause your app to be rejected by the AppStore (In fact, the plugin has a disclaimer to the same effect too).

For waking up the app tomorrow, I simply set a setTimeout

setTimeout(function(){
      console.log('waking up');
      self.helper.scheduleLocalNotification('Hello World', 'Good Morning', 10, "");
      self.ionViewDidEnter();
    }, wakeupinMilliSeconds);

Here is my geolocation code:

setupBackGroundGeolocation(){
      let config = {
                desiredAccuracy: 100,
                stationaryRadius: 50,
                distanceFilter: 100,
                interval: 5000,
                pauseLocationUpdates: false,
                debug: false, //  enable this hear sounds for background-geolocation life-cycle.
                stopOnTerminate: false, // enable this to clear background location settings when the app terminates
        };

        BackgroundGeolocation.configure((location) => {
            console.log('[js] BackgroundGeolocation callback:  ' + location.latitude + ',' + location.longitude);
            this.currentLocation.lat = location.latitude;
            this.currentLocation.lng = location.longitude;

            Promise.all([
                   //I do some calculations here.
              ]).then(d => {
                // IMPORTANT:  You must execute the finish method here to inform the native plugin that you're finished,
                // and the background-task may be completed.  You must do this regardless if your HTTP request is successful or not.
                // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
                BackgroundGeolocation.finish(); // FOR IOS ONLY
              });
        }, (error) => {
          console.log('BackgroundGeolocation error');
        }, config);

        // Turn ON the background-geolocation system.  The user will be tracked whenever they suspend the app.
        BackgroundGeolocation.start();    
      }

I don't use this plugin but had same symptons. I couldn't figure out what was wrong: no error messages, no clues but app kept closing after a few hours.

I guess there was something messed up after installing and uninstalling so many cordova plugins. Now the app is much more stable. I removed and added platform. That seemed to do the job.

I've been reading about ionic2 and performance. Among so many reasons, a possibility about low performance and crash is related not to unsubscribe from observables. Read about async pipe and .unsubscribe observables when component is destroyed ngOnDestroy

Another problem i found was a very basic mistake developing in angular. I loaded everything in the app module so this required lot of memory to load the entire app at once. I guess with slow terminals that can affect more. Anyway, basic angular concepts as .module files should be understood.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM