0

I am trying to send push notification to the users and trying to send 3 times or 2 times a day but after that i don't want to send the notification like i have sent 3 notification today and don't want to send notification after that and next cycle starts from next day and so on I am unable to stop after the 7pm i wanted to stop the node cron after 7 pm and start the cycle next day.

const cron = require("node-cron");
const { db, growthfile20724db } = 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 sendReminderRet = 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("* * /3 * * *", () => {
    console.log("running a task every ten 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 = { sendReminderRet };
mikkel Reng
  • 113
  • 1
  • 9
  • node-cron is not going to work well in Cloud Functions because server instances don't stay alive forever. If you want to schedule a Cloud Function, you should use Cloud Scheduler to invoke the function on your schedule. – Doug Stevenson Jul 04 '20 at 15:27
  • @mikkel Can you please describe your desired functionality? For example, what starts the notification cycle? When should users receive messages? What is a "notification cycle"? Why are you configuring it for every 3 hours if you don't want to send it every 3 hours? Please explain the user experience, rather than how it is implemented. – John Rotenstein Jul 04 '20 at 23:48

0 Answers0