Firebase functions to trigger if the firebase timestamp reaches the time mentioned in any of the data [duplicate] - firebase

I'm developing an app using React Native and Firebase Realtime database. I want to do something like this.
From the app, I set a time duration and press a button. Then, it writes data to the Firebase. Assume that I set the time duration to 1000000000 ms and press the button. Then, the database looks like this:
schedules
|
|--schedule1
|
|--status: "Running"
|--durationInMS: 1000000000
I want to run a background function that changes the above status to "TimeOver" after 1000000000 ms. This function should be run in the background even if the app is closed:
schedules
|
|--schedule1
|
|--status: "TimeOver"
|--durationInMS: 1000000000
How to do this?

There is no built-in trigger to run after a certain delay that is written in the database.
But you can either build your own scheduler using Cloud Scheduler, or create a regularly running scheduled function that then checks for expired schedules in your database.
Also see:
Cloud Functions for Firebase trigger on time?
How to create cron jobs in firebase programmatically
Executing cloud functions after n seconds on demand
Firebase Functions, run after 15 minutes of user inactivity
How can we get Firestore to trigger an event when a document's Date/Time field matches current time?
Scheduled Cloud Functions Firebase
the blog post How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL)
the article Dynamic Scheduled Background Jobs in Firebase

Related

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.

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

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.

Can we delete an attribute of a document in firestore automatically?

I am working on an android project using java where we have a subscription for pro users. Right now I am using firestore as my database, is there any way that I can automatically change the pro attribute in user from true to false in a specific time automatically as the subscription time end? In my user collection's document, there are attributes is_a_pro and pro_availability_time which is in json (YYYY-MM-DD). Thank you
There is nothing built into Firestore to automatically change documents at a given time.
The most common solution is to schedule a Cloud Function, that runs periodically, and then check which subscriptions have expired.
An alternative to running them periodically is to schedule a Cloud Task for when each subscription expires. For an example of how to do that, see Doug's blog post How to schedule a Cloud Function to run in the future with Cloud Tasks.

Scheduled Cloud Functions Firebase

I am trying to schedule a function to run based on DateTime object field in my Firestore database.
Example, I have a collection of events in the Database and each event has a DateTime field (Start time) for the start of the event. The event also has a list of users that are subscribed to the event.
Is there a way to use Cloud Functions to notify the subscribed users that the event is about to start at the start time of the event or 5 or 10 mins before the event starts?
I am currently using Cloud Functions for notification and its working fine but that is based writes to specific portions of the database, not time event.
Thank you in advance.
The only way to do this with a scheduled Cloud Function for this sort of work is to schedule it run periodically (every 1 minute is the most often), and query for documents that meet the criteria for when you want to send the notification at the moment that the function is currently running. This means you should be looking for documents where the time is in the 5-10 minute near future from the current time.
You can't use scheduled Cloud Functions to delay some work to happen later. But you can integrate with Cloud Tasks for that.

Resources