Any way to improve sending push notification quickly using cloud function and fcm or some alternative?

Viewed 783

I'm working on a Android Chat App and for sending a push notifications for the chat i have deployed a Cloud Function to which is sending notification through Firebase Cloud Messaging. But there's a quite delay in receiving notifications. Is there any alternative for send notification quickly or improving it?

The code for my cloud function:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();
const db = admin.firestore();

exports.sendUserMessageNotification = functions.firestore.document("MessagesCollection/{messageChannel}/Messages/{messageId}").onCreate((snap, context) => {

    const document = snap.data();
    const message = document.message;
    const toUserId = document.toUserId;
    const fromUserId = document.fromUserId;
    const fromUserName = document.fromUserName;
    const isAudioMessage = document.audioMessage;

    db.collection("TokenCollection").doc(toUserId).get().then(document => {

        if(document.exists) {

            var tokenDocument = document.data();
            var payload;
    
            if (isAudioMessage) {
    
                payload = {
                    data: {
                        "title": "Message Recieved",
                        "body" : "Voice Message from " + fromUserName,
                        "type": "Message",                
                    }
                };

            } else {

                payload = {
                    data: {
                        "title": "Message Recieved",
                        "body": "Text Message from " + fromUserName, 
                        "type": "Message"
                    }
                };

            }
    
            return admin.messaging().sendToDevice(tokenDocument.deviceToken, payload).then(response => {
    
                console.log("Message Sent Successfully");
    
            });

        }

    });
});

I'm attaching an image to my cloud function log too maybe it's helpful

Cloud Function Log

1 Answers

If you are referring to last execution visible in the logs it seems to be matter of "cold start".

From the log I can see that previous executions have been performed an hour earlier, so I suppose that the execution environment is often initialized from scratch.

You can read few words about it in Firebase functions Tips & tricks article.

Related