【发布时间】:2021-06-13 18:43:03
【问题描述】:
大家好,我是 Flutter 开发者。
我使用 Flutter 和 Firebase 制作了聊天应用程序,但遇到了一些通知问题。
以下代码是我发送通知的方式,当在 Firebase 数据库中创建消息时,我通过 firebase 函数推送通知。
问题是,它发送通知成功,但有时会延迟几个小时或几天!!
如果哪个没有发送通知,我明白,我的代码有任何错误,
但有时它会延迟......大部分工作正常。
我怎么能理解这种情况?有没有办法管理通知速度?
感谢您的阅读,我会等待您的帮助。
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 flutter push-notification google-cloud-functions firebase-cloud-messaging