如何在 Firebase 控制台之外发出预定的 Firebase 云消息传递通知? [英] How can scheduled Firebase Cloud Messaging notifications be made outside of the Firebase Console?

本文介绍了如何在 Firebase 控制台之外发出预定的 Firebase 云消息传递通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Firebase 控制台内的云消息传递视图下,用户可以创建测试通知.此功能还允许您安排将通知发送到一个或一组设备的时间.

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.

是否可以使用 Firebase 云功能和 Firebase Admin SDK 创建和发送预定 FCM 通知到特定设备?有没有其他方法可以解决这个问题?

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.`);
});

首先,我不确定 node-schedule 如何与 Firebase 云功能交互.日志显示该功能很快终止,我认为这是正确的.操作运行的时间越长,我们的 Firebase 账单中的成本就越高.尽管如此,通知仍然会在预定的时间运行.我对这一切在幕后的运作方式感到困惑.

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.

其次,我在取消这些预定通知时遇到了问题.通知很可能从创建之日起按 2 小时的时间表进行.在 2 小时之前,我希望能够使用更新的预定时间取消/覆盖通知.

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.`);
});

是否可以在 firebase 控制台之外利用 firebase 云消息传递的调度功能?还是我一直在解决这个问题?

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?

推荐答案

一个云函数最多可以运行 9 分钟.因此,除非您使用 node-schedule 的时间比这短,否则您当前的方法将不起作用.即使它可行,或者如果您提前不到 9 分钟进行调度,使用这种方法也非常不经济,因为您将在等待期间为 Cloud Functions 付费.

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:

最近对此的改进是使用 Cloud Tasks API 以编程方式安排要在特定时间使用特定负载调用的 Cloud Functions,然后使用它通过 FCM 发送消息.道格史蒂文森在这里写了一篇很棒的博客文章:如何使用 Cloud Tasks 安排云函数在未来运行(构建一个Firestore 文档 TTL).虽然帖子是关于在特定时间删除文档的,但您也可以将其与之前的方法结合来​​安排 FCM 消息.

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.

最后要注意的一点:虽然 Firebase Cloud Messaging 会在应用程序未处于活动状态时自动处理通知消息的显示,但您也可以使用 仅将其用于交付部分data 消息,然后处理应用程序代码中的所有显示.如果您使用这种方法,您可以立即发送 FCM data 消息以及显示消息的时间,然后唤醒设备以在那个时间显示消息.

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.

这篇关于如何在 Firebase 控制台之外发出预定的 Firebase 云消息传递通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆