首页 > 解决方案 > 使用nodejs每隔3小时发送一次FCM推送通知?

问题描述

我正在尝试向昨天在我们平台上活跃的用户发送推送通知,以便每天早上以自动方式发送一次,这是我能够做到的。但是在 4 小时后发送,我无法思考如何在 4 小时后发送它们,就像我在早上 6 点左右发送推送通知,然后在上午 10 点左右,然后在下午 2 点左右,然后在下午 6 点左右发送,但在下午 6 点之后我不想向用户发送推送通知。我该如何解决这个问题。

基本上,有一次我将查询昨天开始时间和结束时间之间的时间戳,然后我将能够提取昨天的活跃用户,然后我能够获取 devicetoken,然后我只是发送推送通知。但是如何间隔发送请提出任何解决方案

    const start = momentTz()
        .tz("Asia/Kolkata")
        .subtract(1, "days")
        .startOf("day")
        .format();
    
      const end = momentTz()
        .tz("Asia/Kolkata")
        .subtract(1, "days")
        .endOf("day")
        .format();
      const profileSnapshot = await db
        .collection("Profiles")
        
         .where("lastQueryFrom", ">=", momentTz(start).valueOf())
         .where("lastQueryFrom", "<=", momentTz(end).valueOf())
        .get();
    
      const profile = [];
      profileSnapshot.forEach((doc) => {
        profile.push(doc.data().phoneNumber);
//here i will get the device token and i will run the fcm push notification logic
      });
      console.log(profile.length);

标签: node.jsfirebasegoogle-cloud-functionsfirebase-cloud-messagingamazon-sns

解决方案


Firebase 有预定的功能,您可以安排一个间隔来执行您提到的检查并发送推送通知。您可以每 4 小时安排一次。您当然可以在您提到的时间范围之外提前退出该功能。

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  // get active users and send push notification
  return null;
});

推荐阅读