Issue
I have an app where upon adding a new document a FCM notification will be sent to users . What I want to achieve now is to let user select a certain date , then on that specific date , the user should get the notification .
I have read the Scheduled Functions with Cloud Functions documentation which shows ( using cron time format ) :
exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
.timeZone('America/New_York')
.onRun((context) => {
console.log('This will be run every day at 11:05 AM Eastern!');
return null;
});
So , for me I would like something like :
exports.scheduledFunctionCrontab = functions.pubsub.schedule('selected date from flutter
date/time picker ')
.timeZone('America/New_York')
.onRun((context) => {
'SEND OUT FCM notification'
;
return null;
});
So how would I convert Flutter Date/Time like -> 2020-05-31 05:41:42 INTO cron time format -> 5 4 * * sun to send out FCM notification on specific date
Thank you
Solution
First to clear some stuff:
- There is no API to create sheduled notifications. That means ther is no way you can define a notification at one time and it will be send at anothe rone.
- You can't create sheduled cloud functions with a specific time to execute from your client. That means this:
exports.scheduledFunctionCrontab = functions.pubsub.schedule('selected date from flutter
date/time picker ')
won't work either.
The only way to solve your problem is to:
- Create a collection where you save notifications to send and when to send them.
- Create a sheduled cloud function that runs periodicaly for example each 1h and check in the collection if the notifications in the collection are near the current time and if it is time to send them
- Send the notifications in that case
I would recommend to run the sheduled cloud function every 1h if your users don't need it for exact minutes.
Answered By - Tarik Huber Answer Checked By - Senaida (WPSolving Volunteer)