I am working on an angular & firebase application. trying to call firebase function/API that will send an email to the user if the user closes the browser/tab. I tried to execute some code on "beforeunload" event but it is not executing after the browser/tab closed. any suggestions will be very helpful. Thank you.
@HostListener('window:beforeunload', ['$event'])
unloadHandler(event: any) {
this.sendCartInfo()
}
sendCartInfo() {
let data = await this.cartService.getCollectionData();
let mail = await this.auth.sendAbEmail({
cartItems: data.products,
email: data.email,
});
}
async sendAbEmail(cartItems) {
return this.http
.post<any>('https://firebase-url.net/app/auth/abondoned',cartItems, {
headers: { Authorization: `Bearer ${await this.getFirebaseToken()}` },
}).toPromise();
}
export const abEmail = (toEmail: string, data, cart) => {
const msg = {
to: toEmail,
from: {
email: "noreply@abc.com",
name: appName,
},
templateId: template_ids.dummy,
dynamic_template_data: { ...data, cart: cart.cartItems },
};
return sgMail.send(msg);
};
export const cronFunction = functions.pubsub
.schedule(`every 2 seconds`)
.onRun((context) => {
sgMail.send(msg);
});