Executing necessary code when App is started in the background by iOS for location changes (Ionic/Angular)

Hi,

My Ionic Angular App enables location tracking and location changes are saved in Firebase. With location permission set to Always, if the App is closed, iOS starts the App in the background when a location changes by a certain distance. However when the App is started in the background by iOS I have found that the normal path of starting the App, through AppComponent code, is not executed and my Firebase connection is not setup (as it would in a normal foreground startup) to save the new location there.

So my questions are -

  1. Is this the expected behavior that the App started by iOS in background won’t execute the AppComponent code
  2. When the App starts in the background how do I execute the needed code to setup my Firebase connection and save the changed location there.
  3. What code is executed when the App is started in the background by iOS when triggered by location change?

I am on Ionic and Capacitor version 7.

Thank you for your guidance.

Sanjay.

When your app is launched in the background by iOS due to a location event, it is not a normal cold start. Instead, iOS wakes the app in a suspended/background state, and your usual platform.ready() or app lifecycle hooks (ngOnInit) may not behave as expected.

  1. Use the BackgroundMode plugin: While it helps keep the app running in the background, it won’t guarantee execution on cold-start unless combined with other tactics

  2. Configure location as a UIBackgroundModes key in your Info.plist:

<key>UIBackgroundModes</key>
<array>
  <string>location</string>
</array>

this will make sure the system knows your app wants to react to location changes even when not in the foreground.

  1. Set up persistent services using native plugins like @mauron85/cordova-plugin-background-geolocation

These plugins have built-in support for handling iOS background triggers and can fire events that you can listen to even if the app wasn’t manually opened.

  1. Add background bootstrap script: to check if the app is being opened by iOS in the background
document.addEventListener('deviceready', () => {
  if (cordova.plugins.backgroundMode) {
    cordova.plugins.backgroundMode.enable();

    cordova.plugins.backgroundMode.on('activate', () => {
      // Safely initialize critical services
      yourLocationService.startTracking();
    });
  }
});
  1. Capacitor/Native bridge workaround: If you’re on Capacitor, consider creating a small native plugin that listens for the specific launch reason and calls your JavaScript logic via the bridge. We did this for a client who needed precise control on background starts for route-tracking.

Thank you for the information @ImperoITservices. Is there some documentation somewhere that you can point me to that talks about what to expect and what code gets executed for an Ionic Angular App when the App is started in the background by iOS (and Android if there is documentation there), triggered by location change, so I can better understand the details of code execution paths?

Yes.

  1. iOS
  1. Android