Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

android - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...