如何向用户nodejs发送fcm推送通知?

时间:2020-07-03 06:42:52

标签: node.js triggers google-cloud-functions amazon-sns

我想在用户要发送推送通知4小时后在我们的应用程序中签入时向用户发送推送通知。我创建了一个端点配置文件,我在其中接收了该配置文件,例如,我已在上午10:00在应用程序中签入,然后我想在2:00 PM和6:00 PM发送推送通知,之后我不想在当天发送任何通知。 我正在尝试使用aws fcm push通知创建了一个主题,在我的项目中我也使用onWrite触发器,但是不知道如何解决此问题。

const profile = async (req, res) => {
  try {//this is the endpoint where i receive the profile and i wanted to pass a function called 
sendReminder(req.body)

}
catch(error){
console.error(error)}
})

在sendReminder函数中,我想执行通知逻辑,但是我认为只要有新的配置文件出现,它就会执行。所以我真的很困惑,从何处开始

这是我的sendReminer代码,目前我正在使用59分钟,但我想使用4个小时或3个小时,它可以工作,但是我想在7:00 Pm之后关闭执行。

const cron = require("node-cron");
const { db } = require("../constant");
let AWS = require("aws-sdk");
// const { getAuth } = require('../utils/reportUtils');
AWS.config.update({
  accessKeyId: "",
  secretAccessKey: "",
  region: "us-east-1",
});
const sns = new AWS.SNS();

const sendReminder = async (profile) => {
  console.log(profile);
  const updateSnapshot = await db
    .collection("Updates")
    .where("phoneNumber", "==", profile.phoneNumber)
    .get();
  const updateArray = updateSnapshot.docs.map(
    (doc) => doc.data().registrationToken
  );
  console.log("registerToken", updateArray[0]);

  cron.schedule("*/59 * * * *", () => {
    console.log("running a task every 59 minutes");
    let params = {
      PlatformApplicationArn:
        "",
      Token: `${updateArray[0]}`,
    };

    let payload2 = JSON.stringify({
      default: "push notification",
      GCM: JSON.stringify({
        notification: {
          body:
            "Hi ",
          title: "Reminder",
        },
        // data: {
        //   testdata: "Check out these awesome deals!",
        //   url: "www.amazon.com",
        // },
      }),
    });
    sns.createPlatformEndpoint(params, (err, data) => {
      if (err) {
        console.log(err);
      } else {
        sns.publish(
          {
            Message: payload2, // Required
            MessageStructure: "json",
            TargetArn: data.EndpointArn,
          },
          (err, data) => {
            if (err) {
              console.log(err.stack);
            } else {
              console.log(data);
            }
          }
        );
      }
    });
  });
};
module.exports = { sendReminder };

0 个答案:

没有答案