Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...