How to schedule an update/notification using firebase cloud functions with dynamic frequency? - firebase

I have an app, where I have an admin and a customer.
Admins can invite users that are in a line.
Admins can configure maximum waiting time for which a customer should show up after he gets invited.
If he does not show up in specified period of time I need to update the database to set the status of a customer's ticket to "no-appearance" and send notification to a customer.
How can I do that using firebase cloud functions?
It looks like I cannot setTimeout because functions are limited to 540 seconds and it is not applicable in my case.
Looks like Pub-sub functions can only be created with a specific period which you have to determine statically

You can use Google Cloud Task to send a scheduled job to trigger cloud functions. Here an article about that: https://medium.com/swlh/event-driven-scheduled-jobs-using-gcp-cloud-tasks-e712c760ab95
The difference of a Pub/Sub is that you can send a task with a dynamic schedule and that task runs only once.

Related

how to schedule a Firebase Cloud function to run in 5 minutes on a specific firestore document upon creation

I would like to make a call to a payment provider's verification API to check the status of the transaction.
The problem.
The payment provider sends a webhook with the status of the transaction but sometimes this process fails. I typically update the status of the transaction in the firestore document upon receipt of the webhook but sometimes this fails leaving me with incoherent statuses in both the payment gateway's console and the firestore document.
Proposed solution.
Upon the creation of a document in firestore, trigger pubsub with a schedule that will run in 5mins.
When the scheduled time arrives, trigger cloud function to run the verification code and update the firestore document.
My request.
Any suggestions and code samples on how to accomplish this will be appreciated.
Thank you.
I tried a cloud function that runs periodically on ALL the documents to confirm the ones that have not been updated yet. this is rather an expensive approach seeing that the amount of documents to read and verify is massive. Also, the payment gateway's verification API only accepts a limited amount of request per second.
For this type of runAfter type functionality, have a look at enqueueing functions in Cloud Tasks in the documentation.
Also see:
Running a scheduler for each user in Firebase Cloud Functions?
run some code 1 hour after a document is updated with cloud function and firestore

scheduling push notification in firebase with cron job

I am building an appointment booking app, I'm using FCM to handle push notifications,
so I am wondering if there is a way to schedule push notifications
For example;
When the doctor gives an appointment time, the patient should be notified few minutes to the appointment time.
I've read about cron job but I'm not sure on how to schedule it to a dynamic time.
You can either run a scheduled task at a fixed interval (say every minute) and then send all messages for the past/next minute, or you can use Cloud Tasks to schedule the task (which is then a HTTP Cloud Function) either yourself (as Doug explained here) oras shown in this documentation.
This topic has been covered quite a few times before, so I recommend also checking out:
How to create cron jobs dynamically in firebase
How to trigger cloud function based on date stored in cloud firestore?
How to trigger a cloud function based on a timestamp attribute of a firestore document
run some code 1 hour after a document is updated with cloud function and firestore

Flutter firebase change a field after 1 year

I am building a feature where user subscribe to app by making payment, it sets a value in cloud Firestore like {"subscribed": true}. I want to set the value false after one year. Can you anyone show me an example how to do it ?
There's no way you can directly use Firestore to schedule any task in the future. If you're not running any custom backend service, then you probably have to look into doing it with Cloud Tasks (Cloud Functions to trigger them).
You can create a cloud function to schedule the Task and one function to handle the task (this will include the logic to update the subscribed flag to false again.

I want to schedule fcm notification for specific user when he add a task to database, flutter

Actually what I am working on a to-do task manager app. Currently I am adding tasks to cloud Firestore after as a new document. I want to send notification to user at the time user scheduled to do that work. I am so confused. It can be lots of tasks from a single user. And the the notification tone has to be selected by user itself.
Please help me in this..
Thanks you!
So you'll need:
A database where you keep the notification payload, user FCM token, and the time they are to be sent.
A schedule Cloud Function that runs every day or every hour based on the granularity of the task.
Now, when the Cloud Function gets triggered, it:
Runs a query against the database for notifications that are scheduled to be sent before the current time
Loops over the results send each notification in turn.
You can now delete the database entry if you want to.
Furthermore, if you want to reduce the number of cloud function triggers, you have a combination of the FCM and an alarm manager to show a notification after a delay (feasible in flutter through local notifications too). this way you can send the payload at a particular time of the day and deploy it at any time locally.
Not sure how optimized this is, looking for better ways!

Firebase Pub/Sub trigger multiple jobs with messages at once

I am running a cronjob every evening to send notifications to a set of users in my database, call them 1-100. However, recently with an increase in my users, it's taking longer and longer for this job to run. Is there a way to have the pub/sub topic trigger multiple jobs, say triggering one job to handle users 1-50, and another to handle users 51-100?
Sure, just partition your users according to some bucketing scheme (maybe modulo their UID), create more pubsub topics and functions for those partitions, and send messages to those topics for each of the the partitions of users that you want to process. So, in your main scheduled function, just send messages to each pubsub topic partition, and let the delegate functions do their work.
Also, if your haven't already, consider increasing the timeout for the main function in case it just needs more time to finish processes. The max timeout for a Cloud Functions background trigger is 9 minutes.

Resources