如何在Firebase控制台之外发出计划的Firebase Cloud Messaging通知? [英] How can scheduled Firebase Cloud Messaging notifications be made outside of the Firebase Console?

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

问题描述

在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:

  • Firebase scheduled notification in android
  • How to schedule push notifcations for react native expo?
  • Schedule jobs in Firebase
  • Ionic: Is it possible to delay incoming push FCM push notification from showing to my device until a specific time
  • Cloud Functions for Firebase trigger on time?
  • How to create cron jobs dynamically in firebase

对此的最新改进是使用Cloud Tasks API以编程方式计划要在特定时间使用特定有效负载调用的Cloud Functions,然后使用该功能通过FCM发送消息.道格·史蒂文森(Doug Stevenson)在这里写了一篇很棒的博客文章:

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将在应用程序不活动时自动处理通知消息的显示,但您也可以使用仅将其用于交付部分.数据消息,然后处理所有在应用程序代码中显示的内容.如果使用该方法,则可以随时间传递FCM data 消息以立即显示该消息,然后在那时唤醒设备以 display 消息

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 Cloud Messaging通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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