1

I'm trying to build a campaign that sends a random message from an array of 10 or 100 messages every day. Currently, in Firebase, I'm able to create a daily campaign that sends the same message daily. Is it possible to use Cloud Functions to change the message of the campaign every day?

mmmm
  • 41
  • 6
  • 1
    Yes, you can use [Firebase Scheduled Functions](https://firebase.google.com/docs/functions/schedule-functions) that'll run at the interval that you specify. In the function you can add the array of N messages and then [get a random item from array](https://stackoverflow.com/questions/5915096/get-a-random-item-from-a-javascript-array) and send it via [Admin SDK](https://firebase.google.com/docs/cloud-messaging/send-message). If you face issues with anything particular, then do post a question with complete details including your code and errors. – Dharmaraj Sep 11 '22 at 19:42

1 Answers1

3

You can use Firebase Scheduled Functions that'll run at the interval that you specify. In the function you can add the array of N messages and then get a random item from array and send it via Admin SDK.

exports.scheduledFunction = functions.pubsub.schedule('0 0 * * *').onRun((context) => {
 
  const messages = [];

  // 1. get a random message from array
  // 2. send notification via FCM (Admin SDK)

  // terminate the function
  return null;
});
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84