If you are looking for something that can execute the given code at a specific time, like scheduling a job, Here is the scheduler library node-schedule. By using this, you can easily create your scheduled jobs in firebase cloud function
. It increases the dependency but still, it's helpful to easily complete the task. still if you want to make the firebase function stateless, You have to go through the second option. Here is the minimal example of this library and cloud function.
1. Using node-schedule
const functions = require('firebase-functions');
var schedule = require('node-schedule');
exports.scheduleSampleJob = functions.https.onRequest((req , res) => {
/*
Say you very specifically want a function to execute at 5:30am on December
21, 2012. Remember - in JavaScript - 0 - January, 11 - December.
*/
var date = new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
console.log('The Task is executed');
});
return res.status(200).send(`Task has been scheduled`);
});
Remember, you need to call this function once only, more than one call will create multiple jobs.
2. Using Firebase Function Pub/Sub
Firebase supports job scheduling as Pub/Sub, but it's not part of Free or Flame plan. To use it you should be running on Blaze plan. Here is what Pub/Sub
for, from the official Docs.
If you want to schedule functions to run at specified times, use functions.pubsub.schedule().onRun()
This convenience method creates a Google Cloud Pub/Sub topic and uses Google Cloud Scheduler to trigger events on that topic, ensuring that your function runs on the desired schedule.
exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
.timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
console.log('This will be run every day at 11:05 AM Eastern!');
return null;
});
3. Using OneSignal Push Notification
If you don't want to manage to schedule manually, You can use OneSignal as an alternative. It has nice push notification provider for Web, IOS, Android and many more platform support and built on top of firebase cloud messaging. It also supports REST-api support and you can schedule push notification using onesignal built-in delivery function. It worth giving a try to this platform rather than implementing it by self. if you choose to go through onesignal, Just create firebase cloud function, which will instantly call OneSignal REST api to schedule the push notification.