【问题标题】:Send a notification every 15 minutes during the day白天每 15 分钟发送一次通知
【发布时间】:2021-04-05 15:46:37
【问题描述】:

我想在我添加到 Firebase 的数据后 1 小时内发送通知,并且我想在添加数据 1 天后取消通知。因为我不懂 JavaScript,而且我还是软件世界的新手,所以我不太清楚它的算法,所以我写了类似的东西。 addedNewCard 方法有效,但我无法调整时间。

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().functions);

var newData;

exports.addedNewCard = 
functions.firestore.document('Users/{userID}/Cards/{cardID}').onCreate(async 
(snapshot, context) => {

const cardID = context.params.cardID;

if (snapshot.empty) {
    console.log('No Devices');
    return;
}

newData = snapshot.data();

const cardAddedDate = newData.cardAddedDate;

const deviceIdTokens = await admin
    .firestore()
    .collection('DeviceToken')
    .get();

var tokens = [];

for (var token of deviceIdTokens.docs) {
    tokens.push(token.data().deviceToken);
}

var payload = {
    notification: {
        title: 'Tekrar vakti',
        body: 'Tekrar etmen gereken kelimeler var!!!',
        sound: 'default',
    },
    data: {
        click_action: 'FLUTTER_NOTIFICATIoN_CLICK',
        sendCardID: cardID,
    }
};
const options = {
    priority: "high",
};

try {
    await admin.messaging().sendToDevice(tokens, payload, options);
    console.log('Notification sent successfully');
} catch (err) {
    console.log(err);
}
 })

exports.timeSettings = functions.pubsub.schedule('every 1 mins').onRun(async 
(context) => {
console.log(context.timestamp);

let now = new Date();

const finishWorking = now.setDate(now.getDate + 1);

const finishWorkingStamp = admin.firestore.Timestamp.fromDate(finishWorking);

db.collection('Users/{userID}/Cards').where('cardAddedDate', '<', 
finishWorkingStamp).get().then((snap) => {
    if (snap.exist) {
        snap.forEach(() => {
            return addedNewCard();
        }).catch((e) => {
            return console.log(e);
        });
    }
});

 })

【问题讨论】:

  • “白天”是什么意思?与夜晚相反吗?如果是这样,您希望调度程序的时间范围是多少?
  • 例如用户添加了一个数据,我想在完成 1 天后发送通知 24 小时我想取消发送通知

标签: javascript firebase google-cloud-functions firebase-cloud-messaging


【解决方案1】:

感谢您的评论,我建议您使用Cloud Task。使用 Cloud Task,您可以在未来延迟执行。

用户发送数据时,可以提前规划24H通知(每15分钟1次通知,持续1天->创建96个任务,下一个比上一个延迟15分钟) .

您有代码示例here。看看这部分,改变延迟

  if (inSeconds) {
    // The time when the task is scheduled to be attempted.
    task.scheduleTime = {
      seconds: inSeconds + Date.now() / 1000,
    };
  }

【讨论】:

    【解决方案2】:

    我不会在客户端执行计划通知,而是在服务器端配置和发送计划。尝试为客户端创建线程以处理通知。

    【讨论】:

      【解决方案3】:

      您必须创建一个 Firebase 云功能,您需要在其中升级您的 Firebase 帐户订阅并使用 pub-sub。

      const functions = require('firebase-functions');
      const admin = require('firebase-admin');
      admin.initializeApp();
      exports.scheduledFunctionCrontab = functions.pubsub.schedule('*/15 * * * *').timeZone('Asia/Kolkata').onRun(async (context) => {
      console.log('This will be run every 15 minutes');
      return null});
      

      【讨论】:

        猜你喜欢
        • 2016-05-03
        • 1970-01-01
        • 2013-10-19
        • 2019-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-09
        • 1970-01-01
        相关资源
        最近更新 更多