'Is there a way to send email digest to a user when they have unread messages/notifications using firebase cloud functions?

I am trying to send emails to users who have unread messages/notifications, but I am struggling to find a way to do it. I was thinking of a system that keeps track of messages/notifications and if they are unread for an hour, then it would send email to the user that will inform them of the messages.

What I found: With cloud scheduler, I will be able to send emails every hour to users that have unread notifications. However, if the user got a notification a minute ago, they will still get an email and that will be annoying for them.

Sending email notification with every message/notifications, with firestore onUpdate will be even more annoying.

Does anyone know if there is a way to do this by tracking notifications/messages as I described on the top? Is there any other way of doing this, that I missed?



Solution 1:[1]

You can use Cloud Tasks to kick off a task for each unread notification, to invoke a function that sends the email for that notification on some delay. You would need to write a fair amount of code to create and configure the task, and the function (perhaps a Cloud Function) to receive the delayed message.

If you want to send emails with batch notifications, you will obviously have to implement some additional logic to make sure that each task sends a rollup rather than an individual message.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Doug Stevenson