I have a Firebase database in which the user can write the date and time of the next workout. How to make the user receive a push notification the day before that he has a workout tomorrow?
I have a Firebase database in which the user can write the date and time of the next workout. How to make the user receive a push notification the day before that he has a workout tomorrow?
2
Answers
You can use flutter_local_notifications package to schedule notifications. Check out scheduling a notification. Or you can use the Cloud Tasks API to programmatically schedule Cloud Functions to be called at a specific time, and then use that to send the message through FCM. Check out (How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL)
The example of using Google Cloud Tasks that the other answer gave works (I’m actually using that exact pattern), but that may be over-complicated for your usecase.
Instead, just schedule a function to run on a one-a-day (or more or less frequent) schedule, check out this documentation. In this function, you can determine which users need reminders, and then send out bulk notification to those users.
You could also run this function a few times a day, allowing the user to select morning, afternoon, or evening notification if you want to allow that, only notifying users who are in that reminder slot.
If you need to customize the exact time the notifications are sent on a per-user basis (as opposed to all users getting the reminder at the same time), or there’s too many users to process and send notifications in bulk in a single function invocation, then using the cloud tasks pattern might make sense.