【发布时间】:2021-02-16 02:08:16
【问题描述】:
您好,我正在使用 Firestore 创建一个聊天应用。我看到了很多关于使用云消息创建徽章通知的信息,但没有看到很多关于创建没有云消息的徽章通知的信息。有谁知道如何做到这一点?当用户收到他们尚未阅读的消息时,我试图在图标上显示一个点。如果我能知道他们还没有读过的消息的总数,那就更好了。
Firestore 结构
users
|
---- chatList (subcollection)
---- chatFrom: user1_Id
---- chatWith: user2_Id
---- chatRoomId: smallerUserID_biggerUserID
chatRooms
|
---- smallerUserID_biggerUserID (subcollection)
---- content: "Hello"
---- id: 1613422354427
---- idFrom: user1_Id
---- timestamp: 1613422354427
在聊天室集合中获取和发送消息
getMessages() {
this.listMessage = [];
db.collection('chatRooms').doc(this.chatRoomId).collection(this.chatRoomId)
.onSnapshot((snapshot) => {
snapshot.docChanges().forEach((change) => {
if (change.type === 'added') {
this.listMessage.push(change.doc.data());
}
});
});
},
async sendMessage(content) {
if (content.trim() === '') { return }
const timestamp = moment().valueOf().toString();
const idFrom = this.authUser.userId;
const idTo = this.currentPeerUser.userId;
const message = { id: timestamp, idFrom, idTo, timestamp, content };
const chatRoomRef = db.collection('chatRooms').doc(this.chatRoomId)
.collection(this.chatRoomId).doc(timestamp);
await chatRoomRef.set(message);
this.inputValue = '';
},
【问题讨论】:
-
我认为您需要在邮件上添加一个附加属性,说明收件人是否阅读了邮件。由前端决定何时读取。
-
我认为我需要它,但我还没有决定如何确定是否有人阅读了一条消息。我遇到的最大问题是当有人在聊天室中并且发送了一条新消息时。我不确定如何判断该消息是否已被阅读。你有什么想法吗?
-
您可以使用 Intersection Observer 来确定消息是否已进入收件人的视口,然后在 Firestore 中更新消息,标记为例如
readByUsers:[user1]?或类似的东西 -
@Alex 你能分享你已有的用于显示新消息的代码吗?
-
@John 这听起来很酷。我将研究交叉路口观察员。感谢您的帮助
标签: javascript firebase vue.js google-cloud-firestore