簡體   English   中英

Flutter / Firebase 延遲推送通知

[英]Flutter / Firebase delayed push notification

您好,我是 Jr Flutter 開發人員。

我使用 Flutter 和 Firebase 制作了聊天應用程序,但我遇到了一些通知問題。

下面的代碼是我發送通知的方式,當在 Firebase 數據庫中創建消息時,我通過 firebase function 推送通知。

問題是,它正在成功發送通知,但有時它會延遲幾個小時或幾天!

如果哪個沒有發送通知,我明白,我的代碼有任何錯誤,

但有時它會延遲......大部分工作正常。

我怎么能理解這種情況? 有沒有辦法管理通知速度?

感謝您的閱讀,我將等待您的幫助。

exports.onCreateMessage = functions.firestore//Notification
    .document('ChatRoom/{chatRoomID}/Messages/{message}')
    .onCreate(async (snap, context) => {
        const chatRoomID = context.params.chatRoomID;
        const message = snap.data();
        const chatRoomRef = await admin.firestore().collection('ChatRoom')
            .doc(chatRoomID).get();

        //setDate to Chatroom
        chatRoomRef.ref.update({
            latestMessageID: message.messageType === CHAT_MESSAGE_TYPE_EMOJI ? '[STICKER]' : message.message,
            latestMessageTime: new Date()
        });
        const senderUserRef = await admin.firestore().collection('User').doc(message.senderID).get();
        //getUserList add  then number;
        const joinedUserList = Object.entries(chatRoomRef.data().joinedUserList);//convert obejct to map.
        joinedUserList.forEach(async (value, key, map) => {
            if (value[0] !== message.senderID) {
                const joinedChatRoomRef = await admin.firestore()
                    .collection('UserJoinedChatRooms').doc(value[0]).collection('JoinedChatRoomList')
                    .doc(chatRoomID).get();
                await admin.firestore()
                    .collection('UserJoinedChatRooms').doc(value[0]).collection('JoinedChatRoomList')
                    .doc(chatRoomID).update({
                        unReadMessageCount: joinedChatRoomRef.data().unReadMessageCount + 1,
                        latestMessageTime: new Date(),
                        isInTheChatRoom: true,
                    });
                return admin.messaging().sendToTopic(`${value[0]}`, {
                    notification: {
                        title: senderUserRef.data().name,
                        body: message.messageType === CHAT_MESSAGE_TYPE_EMOJI ? '[STICKER]' : message.message,
                        clickAction: 'FLUTTER_NOTIFICATION_CLICK',
                        sound: 'default'
                    }
                    , data: {
                        notificationType: message.messageType.toString()
                    }
                });
            }
            else {
                await admin.firestore()
                    .collection('UserJoinedChatRooms').doc(value[0]).collection('JoinedChatRoomList')
                    .doc(chatRoomID).update({
                        latestMessageTime: new Date(),
                    });
            }
        });
    });

firebase通知中沒有速度,只要您將數據提交給firebase並且用戶在線就會顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM