[javascript] code
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- exports.scheduleAsyncMorningNotif = functions.pubsub.schedule('10 08 * * *')
- // exports.scheduleAsyncMorningNotif = functions.pubsub.schedule("every 5 minutes")
- .timeZone("America/Chicago")
- .onRun((context) => {
- const tsToMillis = admin.firestore.Timestamp.now().toMillis();
- const today = new Date(tsToMillis + 15 * 60 * 60 * 1000);
-
- const userPlants = db.collection("UserPlants");
-
- var lastVisible = null;
- // let totalSuccessCount = 0;
- var multipleUsers = [];
-
- function handleUserPlantsFCM() {
- const snapshot = !!lastVisible
- ? userPlants
- .where("nextWatering", "<=", today)
- .where("notificationsEnabled", "==", true)
- .orderBy("nextWatering")
- .startAfter(lastVisible)
- .limit(500)
- .get()
- : userPlants
- .where("nextWatering", "<=", today)
- .where("notificationsEnabled", "==", true)
- .orderBy("nextWatering")
- .limit(500)
- .get();
-
- if (snapshot.size === 0) {
- console.log("No matching documents.");
- return;
- }
-
- snapshot.then(async function (querySnapshot) {
- console.log(
- "Query Snapshot of UserPlants showing as:",
- querySnapshot.size
- );
- if (querySnapshot.size === 0) {
- console.log("No matching documents.");
- console.log(
- "*****************************************Sending Messages*************************************************"
- );
-
- if (!!multipleUsers.length) {
- const matchedMultipleUsers = multipleUsers.filter(
- (plant) => plant.count > 2
- );
- if (!!matchedMultipleUsers.length) {
- const matchedNotifications = matchedMultipleUsers.map(
- (plant) => ({
- notification: {
- title: `You have ${plant.count} plants to water! 🌱 `,
- body: `Your ${plant.plantName.toString()} needs some water - make sure to take care of it! ☔️`,
- },
- token: plant.userFCMToken,
- })
- );
- if (!!matchedNotifications && matchedNotifications.length) {
- try {
- admin
- .messaging()
- .sendAll(matchedNotifications)
- .then((response) => {
- const multipleSuccessCount = response.successCount;
- console.log(
- `${multipleSuccessCount} grouping messages are sent successfully.`
- );
- console.log(matchedMultipleUsers.length, matchedNotifications.length);
- });
- } catch (err) {
- console.log(err);
- }
- } else {
- console.log("There is no any grouping messages to be sent!");
- }
- } else {
- console.log("There is no any user who has multiple plants.");
- }
-
- const matchedOneOrTwoUsers = multipleUsers.filter(
- (plant) => plant.count < 3
- );
- if (!!matchedOneOrTwoUsers.length) {
- const matchedNotifications = matchedOneOrTwoUsers.map(
- (plant) => ({
- notification: {
- title: `You have a plant to save! 🌱 `,
- body: `Your ${plant.plantName.toString()} needs some water - make sure to take care of it! ☔️`,
- },
- token: plant.userFCMToken,
- })
- );
- if (!!matchedNotifications && matchedNotifications.length) {
- try {
- admin
- .messaging()
- .sendAll(matchedNotifications)
- .then((response) => {
- const multipleSuccessCount = response.successCount;
- console.log(
- `${multipleSuccessCount} messages are sent successfully.`
- );
- console.log(matchedOneOrTwoUsers.length, matchedNotifications.length);
- });
- } catch (err) {
- console.log(err);
- }
- } else {
- console.log("There is no any messages to be sent!");
- }
- } else {
- console.log("There is no any user who has a plant.");
- }
- }
- return;
- }
- lastVisible = querySnapshot.docs[querySnapshot.docs.length - 1];
- // var messages = [];
- querySnapshot.forEach((doc) => {
- const data = doc.data();
- const { plantName, userFCMToken, userRef } = data;
-
- if (plantName && userFCMToken) {
- // check if user does not exist in multiple users
- if (!multipleUsers.some((plant) => plant.userRef === userRef)) {
- let arr = [];
- const userObj = {
- userRef: userRef,
- userFCMToken: userFCMToken,
- plantName: arr.push(plantName),
- count: 1,
- };
- console.log("==============NewUserPlant", plantName);
- multipleUsers.push(userObj);
- } else {
- const existingUserPlant = multipleUsers.find(
- (plant) => plant.userRef === userRef
- );
- console.log(
- "++++++++++++++existingUserPlant:",
- existingUserPlant.plantName[0]
- );
- let arr = [];
- const updatedUserPlant = {
- ...existingUserPlant,
- count: parseInt(existingUserPlant.count) + 1,
- plantName: Array.isArray(existingUserPlant.plantName)
- ? existingUserPlant.plantName.push(plantName)
- : [plantName],
- };
- const updatedMultipleUsers = multipleUsers.map((plant) => {
- if (plant.userRef === userRef) return updatedUserPlant;
- else return plant;
- });
- multipleUsers = updatedMultipleUsers;
- }
-
- // const message = {
- // notification: {
- // title: 'You have a plant to save! 🌱',
- // body: `Your ${plantName} needs some water - make sure to take care of it! ☔️`,
- // },
- // token: userFCMToken
- // };
- // messages.push(message);
- } else {
- console.log("Nil/false on user token, skipping for now");
- }
- });
- // if (!!messages.length) {
- // try {
- // const response = await admin.messaging().sendAll(messages);
- // totalSuccessCount += response.successCount;
- // } catch (err) {
- // console.log(err);
- // }
- // messages = [];
- // }
- handleUserPlantsFCM();
- });
- }
- handleUserPlantsFCM();
- return;
- });
Editor
You can edit this paste and save as new:
File Description
- code
- Paste Code
- 22 Jun-2021
- 7.15 Kb
You can Share it:
Latest Code Pastes