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
