简体   繁体   English

有什么方法可以使用云 function 和 fcm 或其他替代方法来改进快速发送推送通知?

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

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.我正在开发一个 Android 聊天应用程序,为了发送聊天的推送通知,我部署了一个云 Function,通过 Firebase 云消息传递向其发送通知。 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:我的云 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我也将图像附加到我的云 function 日志中,也许它有帮助

云功能日志

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.您可以在Firebase 功能提示和技巧文章中阅读有关它的几句话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM