Home > Back-end >  Angular Capacitor: appStateChange listener not working
Angular Capacitor: appStateChange listener not working

Time:10-23

To solve a KPI, i'm trying to generate a logEvent with the Firebase SDK when the user closes the Application (Ionic / Capacitor / Angular). But i'm having a lot of trouble trying to enter at that appStateChange listener (That is being created at ngOnInit method of my app.component.ts).

As the official Capacitor Background Task Plugin was deactivated, i've been using this community plugin right here, that acts just like the official one

  import { BackgroundTask } from '@robingenz/capacitor-background-task';

  App.addListener('appStateChange', async ({ isActive }) => {
       // It isn't getting here

       if (isActive)
         return;
       
       // When the app is running at the background

       const taskId = await BackgroundTask.beforeExit(async () => {
            console.log('Sending app_close event to Firebase')

            await this.firebaseAnalyticsService.logEvent('app_close', {
              lastScreenName: this.firebaseAnalyticsService.getCurrentScreenName(),
            });

            BackgroundTask.finish({ taskId });
       });
  })

  

i've tried to deal with this by other methods too, with cordova plugins, window.beforeUnload, etc... None of them worked as i was expecting (not entering at the handlers). I would appreciate if someone could help me to figure out this one.

CodePudding user response:

Solved! My solution was to increase Capacitor versions and remove the background-task plugin, as it was not needed for my intend.

  • Related