0

I'm new on notifications with firebase and Cloud Functions and wondering if even using CF where servers are managed by Firebase's teams and has autoscale, is there a chance, if I was sending 100.000 notifications to my users, to crash my application?

I'm doing something like that:

  1. Someone on the app made a post;
  2. On my CF has a trigger to send notifications for the users;
  3. Then, all users (100.000) received the notifications.

Is Firebase infrastructure has queue or job manage to avoid this kind of situations or can I sleep well like a babe?

Here a snippet of my code:

exports.newPost = functions.database.ref("post/{postId}")
    .onUpdate((change, context) => {
        const before = change.before.val();
        const after = change.after.val();

        if (before.status === after.status) {
            return null;
        }

        const ref = admin.database().ref(`tokens/`);
        return ref.orderByKey().once("value", async snapshot => {

            const fcmTokens = snapshotToArray(snapshot);

            admin.messaging()
                .sendMulticast({
                    data: {},
                    tokens: fcmTokens,
                    notification: {
                        title: 'Title',
                        body: 'Body',
                    },
                    android: {
                        notification: {
                            image: imageUrl,
                        },
                    },
                    apns: {
                        payload: {
                            aps: {
                                "mutable-content": 1,
                            },
                        },
                        fcm_options: {
                            image: imageUrl,
                        },
                    },
                })
                .then(resp => console.log(resp))
                .catch(e => console.log(e))
        })
    })

My questions is: Is there a possible problem with my code?

4
  • 1
    I'm unclear what your concern is here. FCM sends billions of notifications daily and there are no problems with that, in a general sense. Commented Dec 2, 2020 at 17:11
  • @DougStevenson, I edited my post showing what is my concern Commented Dec 2, 2020 at 18:50
  • I still don't really understand the problem you are anticipating. Commented Dec 2, 2020 at 18:57
  • @DougStevenson, I'd like to know if I send one notification to a hundred thousand users using Cloud Functions with FCM, my backend will handle it without crash my application? I'm using that snippet code.. Commented Dec 2, 2020 at 19:33

2 Answers 2

3

There is nothing inherently "bad" with the code you're showing here. FCM regularly sends billions of notifications daily. The hardest part of that isn't even handled by the code you show here - it's handled by FCM servers. The code here is just telling the servers to send those messages, which is requires extremely low effort.

Sign up to request clarification or add additional context in comments.

2 Comments

Is FCM theres no trigger limitation of number of notifications?
I'm not really sure what you're asking. The limits of FCM are well documented and have nothing to do with Cloud Functions. firebase.google.com/docs/cloud-messaging/concept-options
0

The only problem I could see here is the limit to the tokens which can cause the failure of notifications to be delivered.

You’re using sendMulticast which accepts up to 500 FCM Tokens per invocation.

https://firebase.google.com/docs/cloud-messaging/send-message

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.