Hey there! I am currently developing a chat application using Firestore. While researching, I found plenty of information on creating badge notifications with cloud messaging, but not much without it. Can anyone help me out with this? I want to display a notification dot on an icon when a user receives an unread message. It would be great if I could also show the total number of unread messages.
Firestore Data Structure
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
Fetching and Sending Messages in chatRooms Collection
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 = '';
},