3

I'm looking for a way to send a firebase cloud messaging notification on a specific time using cloud functions.

At the moment, a notification is sent as soon as a new document is created in a Firestore database, and he time that the notification should be sent on comes from a timestamp in that document.

I have looked at creating a cron job, but I can't find a way to only send the notification once, and not once every day / week. I also looked at using a setTimeout function, but that seems like a bad idea in this scenario because of the time limit every invocation has.

I have very little experience with both typescript and firebase cloud functions, so i would be glad if someone has an idea on how to do this!

Hannes Hultergård
  • 1,157
  • 1
  • 11
  • 33

1 Answers1

3

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.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
  • 2
    I would expect this **not** to work reliably, as Cloud Functions sever instances are ephemeral, and will be deallocated in various circumstances. Cloud Functions code should be stateless and should not expect to run outside of a single function execution. – Doug Stevenson Oct 04 '19 at 15:08
  • 1
    That's what I'm worried about as well. This probably works if the time is in the very near future, but not if it's several hours away. – Hannes Hultergård Oct 04 '19 at 15:25
  • The pure implementation may not help for the long period(i.e after one year) and that's right. but I also posted another solution built on top of firebase-messaging. It has all you need. All you need to do is just call the API of OneSignal from the firebase clude function and schedule the push notification. – Kiran Maniya Oct 05 '19 at 00:48