In my quest to solve a KPI, I am attempting to trigger a logEvent using the Firebase SDK when the user closes the application (specifically in an Ionic/Capacitor/Angular environment). However, I am facing numerous challenges trying to access the appStateChange
listener that is initialized in the ngOnInit
method of my app.component.ts
.
Since the official Capacitor Background Task Plugin has been disabled, I have resorted to utilizing this community plugin available here, which serves a similar purpose.
import { BackgroundTask } from '@robingenz/capacitor-background-task';
App.addListener('appStateChange', async ({ isActive }) => {
// Unable to reach this point
if (isActive)
return;
// Handling app running in 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 have also explored other methods like Cordova plugins and window.beforeUnload, but none have provided the desired outcome (i.e., not triggering the handlers). Any assistance in resolving this issue would be greatly appreciated.