Home > database >  How to choose a random message from an array for Firebase Cloud Messaging Campaign
How to choose a random message from an array for Firebase Cloud Messaging Campaign

Time:09-12

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?

CodePudding user response:

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;
});
  • Related