简体   繁体   中英

Send push notification at specific time with Firebase and Cloud functions

I created a plateform where fitness coaches can publish workout to their clients. They have a planning of the week, they can fill the week entirely but the workouts will be released only day by day at 00:00.

I would like to add a feature: configure push notifications at a specific time (the coaches will decide when) everyday when there is a workout. For example every morning at 08:01 or every afternoon at 5:35

  • I know that it's possible to set scheduled functions with firebase, but I'm not sure is the right way to do it.

Indeed, if I use this logic, I should schedule a function every minute of the day (It will be too much). Is there a way to launch a function everyday at 00:00 for example, and that can look in my database if coaches enabled this feature. Then, launch push notifications with delay

(Ex: My function see that a coach wants to send notifications at 08:00 and another one at 09:30. The function could send push notifications with a delay of 8hours and another time with a delay of 9hours and 30 minutes)

Do you have any idea about how I could implement this feature?

I've implemented something similar in an app I have. I use the Cloud Scheduler in Firebase and run it every 10 minutes.

When my user submits a message for a specific time, I record that time in the database (Realtime Database).

The cloud function launched by the cloud scheduler loops through that database, looking at the time the user has entered. If the time is within the last 10 minutes, then the message is sent and I remove the entry from that table.

To call the cloud function with the scheduler you create the function as follows:

exports.findScheduledEvents = functions.pubsub.topic('eventSchedule').onPublish(() => {

where eventSchedule is the name of my Cloud Schedule.

Firebase only charges you for the number of cloud schedules you have, not how often you run them. For me, very 10 minutes is sufficient, but you could run one every minute it you want. Just be careful not to make the table that is looped through too large. As I said, I remove the entry so that particular table never gets too big and it costs very little.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM