I have a strange problem with an HTTPS Cloud Function that based on specific rules send push notifications to client. In particular, when the Cloud Function starts for the first time, notifications are sent with a delay. On the contrary, after Cold Start, everything works as expected, the push notification is sent straight after.
My code looks like the following (very simplified):
exports.myCloudFunction = functions.https.onRequest(async (request, response) => {
try {
const numberOfImportedItems = await importData();
if (numberOfImportedItems > 0) {
await sendPushNotification();
}
response.status(200).send('OK');
} catch (error) {
response.status(404).send('KO');
}
});
where sendPushNotification
is defined the following:
async function sendPushNotification() {
// https://firebase.google.com/docs/functions/tips?hl=en#use_dependencies_wisely
const module = await import('push-notification-module');
// compose the notification here
module.send(notification);
}
It's worth to note that import('push-notification-module')
it's not a top level import but a local one since the in certain cases notifications are not sent (based on numberOfImportedItems
).
Do you have any suggestion why this happens? Do you have any suggestion on how to mitigate this delay behavior?