How can scheduled Firebase Cloud Messaging notifications be made outside of the Firebase Console?

12,844

A Cloud Function can run for a maximum of 9 minutes. So unless you're using node-schedule for periods shorter than that, your current approach won't work. Even if it would work, or if you are scheduling for less than 9 minutes in advance, using this approach is very uneconomic as you'll be paying for the Cloud Functions for all this time while it's waiting.


A more common approach is to store information about what message you want to be delivered to whom at what time in a database, and then use regular scheduled functions to periodically check what messages to send. For more on this, see these previous questions:


A recent improvement on this is to use the Cloud Tasks API to programmatically schedule Cloud Functions to be called at a specific time with a specific payload, and then use that to send the message through FCM. Doug Stevenson wrote a great blog post about this here: How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL). While the post is about deleting documents at a certain time, you can combine it with the previous approach to schedule FCM messages too.


One final thing to note: while Firebase Cloud Messaging will automatically handle the display of notification messages when the application isn't active, you can also use it for only the delivery part using data messages and then handling all display in your application code. If you use that approach, you can deliver the FCM data message straight away with the time to display the message, and then wake the device up to display the message at that time.

Share:
12,844
Tristan Heilman
Author by

Tristan Heilman

Updated on July 30, 2022

Comments

  • Tristan Heilman
    Tristan Heilman over 1 year

    Inside the Firebase Console, under the Cloud Messaging view, users are able to create test notifications. This functionality also allows you to schedule the time at which the notification will send to a device or set of devices.

    Is it possible to create and send scheduled FCM notifications to specific devices by using firebase cloud functions and the Firebase Admin SDK? Is there an alternative way to solving this?

    The current way that I send scheduled messages to users is like so:

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    const schedule = require('node-schedule');
    admin.initializeApp();
    
    exports.setScheduledNotification = functions.https.onRequest(async (req, res) => {
        const key = req.query.notification_key;
    
        const message = {
            notification: {
                title: 'Test Notification',
                body: 'Test Notification body.'
            }
        };
    
        var currentDate = new Date();
        var laterDate = new Date(currentDate.getTime() + (1 * 60000));
    
        var job = schedule.scheduleJob(key, laterDate, () => {
            const snapshot = admin.messaging().sendToDevice(key, message);
        });
    
        return res.status(200).send(`Message has been scheduled.`);
    });
    

    First of all, I am unsure how node-schedule interacts with firebase cloud functions. The logs appear that the function terminates very quickly which I would think would be correct. The longer the operation runs the more costly it is in our firebase bills. The notification does still run on a scheduled time though. I'm confused on how that all is working behind the scenes.

    Secondly, I am having issues canceling these scheduled notifications. The notifications will most likely be on a 2hr timed schedule from the point it gets created. Before the 2hrs is up, I'd like the have the ability to cancel/overwrite the notification with an updated scheduled time.

    I tried this to cancel the notification and it failed to find the previously created notification. Here is the code for that:

    exports.cancelScheduledNotification = functions.https.onRequest(async (req, res) => {
        const key = req.query.notification_key;
    
        var job = schedule.scheduledJobs[key];
        job.cancel();
    
        return res.status(200).send(`Message has been canceled.`);
    });
    

    Is it possible to tap into the scheduling functionality of firebase cloud messaging outside of the firebase console? Or am I stuck with hacking my way around this issue?

  • Tristan Heilman
    Tristan Heilman about 4 years
    I ended up utilizing the Cloud Tasks API to schedule the Cloud Functions. I appreciate the help!
  • Viktor Vostrikov
    Viktor Vostrikov almost 4 years
    @TristanHeilman, can you share more about this? Perhaps some example on how to trigger FCM with Cloud Tasks API?
  • Dwagh
    Dwagh over 3 years
    Out of curiosity, if FCM does not support scheduled notifications, how does the Firebase Console solve the problem of scheduled notifications.
  • Frank van Puffelen
    Frank van Puffelen over 3 years
    The FCM API doesn't support scheduled messages. The Firebase console uses its own proprietary mechanism for scheduling notifications.
  • Charles Van Damme
    Charles Van Damme almost 2 years
    @FrankvanPuffelen is it not possible to do this by setting an event_time property?
  • Frank van Puffelen
    Frank van Puffelen almost 2 years
    No, the FCM API doesn't support scheduled messages.