Search code examples
firebasecronfirebase-hosting

Scheduling the javascript script hosted on firebase to run in every 30 minutes


I have written a javascript code, which contains all the necessary firebase database conditioning,on the basis of which I am sending push notifications using fcm. I have hosted the script (index.html & firebase.js) on the firebase. I want to schedule the script kept on server at every 30 minutes(it should run automatically), so that conditions written on script will be checked in every 30 minutes and push notifications will be sent automatically to the user. Kinldy, help me to solve this issue. Thanks.

I have heard of cron task. But I don't know how to implement this with the script.

I want that the push notification should be sent to the user based on the conditions that i have written in the script.

How to schedule this script on server side?


Solution

  • There is no way to schedule running code on Firebase Hosting. In fact: Firebase Hosting doesn't run any of your code. All code in your index.html and JavaScript are sent to any client that connects to your site, and are interpreted there. Which is actually a security risk in your current approach.

    Sending FCM messages requires that you specify the FCM server key. As its name implies, this key should only be used in a trusted environment, such as a server you control, your development machine, or Cloud Functions. The latter is probably the best way for you to send these FCM messages, as you can schedule Cloud Functions to run on an interval.